A Pen by Andy Terranova on CodePen.
Last active
March 29, 2022 18:48
-
-
Save supernova-at/35276c97736059c9cfdc to your computer and use it in GitHub Desktop.
Fizz Buzz Bang
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
// From 1-100 inclusive | |
for (var i = 1; i <= 100; i++) { | |
// Start with an empty string | |
var output = ''; | |
/* | |
* Fizz: Is it a multiple of 7? | |
* If divide by 7 remainder is zero, then yes | |
*/ | |
if (i % 7 === 0) { | |
output += 'fizz'; | |
} | |
/* | |
* Buzz: Does it have a 7 in it? | |
* Two ways this can happen: tens digit or ones digit | |
*/ | |
if (i % 10 === 7 || // Check the ones digit first because it's less expensive | |
parseInt(i / 10) === 7) // Check the tens digit (parseInt removes decimal) | |
{ | |
output += 'buzz'; | |
} | |
/* | |
* Bang: Is it a double? | |
* In other words, is it a multiple of 11? | |
*/ | |
if (i % 11 === 0) { | |
output += 'bang'; | |
} | |
/* | |
* Did it not satisfy any of the conditions? | |
* Then just output the number itself | |
*/ | |
if (output.length === 0) { | |
output = i; | |
} | |
// Actually print it out | |
console.log(i + ' = ' + output); | |
} |
That's a really good question - probably not. Might just suck it up and go the toString
route. Even though it's more expensive, it easily simplifies out to higher limits.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like what you did for buzz. Would you do something similar if the limit was higher, say 10,000 instead of 100?