How to make the smallest possible JavaScript FizzBuzz solution
How to make the smallest possible JavaScript FizzBuzz solution. Requirements:
Print numbers from 1 to 100
If a number is dividable by 3, print “Fizz” instead
If a number is dividable by 5, print “Buzz” instead
If a number is dividable by 3 and 5, print “FizzBuzz” instead
Print using console.log
Pure JavaScript only
1. //////////////////////////////////// First Try (101 characters)
for(n=1;n<=100;++n){s="";if(!(n%3))s="Fizz";if(!(n%5))s+="Buzz";else if(n%3&&n%5)s=n;console.log(s);}
2. //////////////////////////////////// Try (89 characters)
for(n=1;n<101;++n)console.log(((n%3)?'':'Fizz')+((n%5)?'':'Buzz')||n)
3. Others solutions :
////////////////////////////////////////////////////////////////////////////
var i, output;
for (i = 1; i < 101; i++) {
output = '';
if (!(i % 3)) output += 'Fizz';
if (!(i % 5)) output += 'Buzz';
console.log(output || i);
}
////////////////////////////////////////////////////////////////////////////
// fizz Buzz code here.
for (var i=0; i <=100; i++) {
if (i%3 === 0 && i%5 === 0)
console.log("FizzBuzz");
else if (i%3 ===0)
console.log("Fizz");
else if (i%5 ===0)
console.log("Buzz");
else
console.log(i);
}
//////////////////////////////////////////////////////////////////////////////
// Eloquent JS solution
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output += "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
////////////////////////////////////////////////////////////////////////////
// More practice solutions:
for(i=0;i<100;)console.log((++i%3?'':'Fizz')+(i%5?'':'Buzz')||i)
////////////////////////////////////////////////////////////////////////////
for (var i = 1; i <= 100; i++) {
var f = i % 3 == 0, b = i % 5 == 0;
console.log(f ? b ? "FizzBuzz" : "Fizz" : b ? "Buzz" : i);
}