Skip to content

Instantly share code, notes, and snippets.

@bryantee
Created June 5, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryantee/81ecaa6487ba56553ce9 to your computer and use it in GitHub Desktop.
Save bryantee/81ecaa6487ba56553ce9 to your computer and use it in GitHub Desktop.
FizzBuzz refactor functions
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<div class="fizzbox">
<ul>
</ul>
</div>
</body>
</html>
$(document).ready(function() {
// global variable
var highNumber
// declare some functions
function fizzBuzz(number) {
console.log('Let\'s Buzz all the way to ' + number);
//start the loop - the real fizzbuzz logic here
for (var i = 1; i < (number + 1); i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log('FizzBuzz');
printTo('.fizzbox ul', 'FizzBuzz');
}
else if (i % 5 === 0) {
console.log('Buzz');
printTo('.fizzbox ul', 'Buzz');
}
else if (i % 3 === 0) {
console.log('Fizz');
printTo('.fizzbox ul', 'Fizz');
}
else {
console.log(i);
printTo('.fizzbox ul', i)
}
}
};
function getNumber() {
highNumber = prompt('Choose a high number to use in the FizzBuzz game: ');
highNumber = parseInt(highNumber + 'a');
console.log(highNumber);
};
// append to div
function printTo(div, value) {
value = '<li>' + value;
value += '</li>';
$(div).append(value);
}
// run getNumber function first time
getNumber();
// validate highNumber is a number
// if not, re-prompt
while (isNaN(highNumber)) {
alert('You didn\'t enter a number. Please try again.');
getNumber();
}
fizzBuzz(highNumber);
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
body {
font-size: 1.3em;
}
.fizzbox ul {
list-style-type: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment