Skip to content

Instantly share code, notes, and snippets.

@djtriptych
Created January 12, 2012 06:59
Show Gist options
  • Save djtriptych/1599120 to your computer and use it in GitHub Desktop.
Save djtriptych/1599120 to your computer and use it in GitHub Desktop.
FizzBuzz implementations with no if/else statements.
// Do 'fizzbuzz' for numbers from 1 to n, no if/else
var fizzbuzz = function (n) {
var i = 0;
while (i++ < n)
console.log(i, !(i%15)&&'fizzbuzz'||!(i%5)&&'buzz'||!(i%3)&&'fizz'||i);
};
// Do fizzbuzz recursively (no loop structures)
var fizzbuzz_r = function (n) {
if (!n) return;
fizzbuzz(n-1);
console.log(n, !(n%15)&&'fizzbuzz'||!(n%5)&&'buzz'||!(n%3)&&'fizz'||n);
}
// Do fizzbuzz with fewer tests (no need to check for 15)
var fizzbuzz_s = function (n) {
var i = 0;
while (i++ < n)
console.log(i, (!(i%3)?'fizz':'')+(!(i%5)?'buzz':'')||i);
}
// uncomment to test...
// fizzbuzz(20);
// fizzbuzz_r(20);
// fizzbuzz_s(20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment