A Pen by Bryan Swagerty on CodePen.
Created
June 5, 2015 20:50
-
-
Save bryantee/81ecaa6487ba56553ce9 to your computer and use it in GitHub Desktop.
FizzBuzz refactor functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<div class="fizzbox"> | |
<ul> | |
</ul> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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