A < 140 byte solution to the classic "FizzBuzz" problem.
The Fizz Buzz problem I'll work with is defined as follows:
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz.” For numbers which are multiples of both three and five print “FizzBuzz.”
For more information, see the page at http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html.
@danbeam on Twitter previously solved this problem in 84 Bytes. His solution was as follows:
for(i=0;++i<101;)console.log(!(i%15)&&"fizzbuzz"||!(i%3)&&"fizz"||!(i%5)&&"buzz"||i)
My solution solves it in 67. Beat that, ninja's :)
Rule #3 of 140byt.es is does not leak to the global scope. Therefore your code should be:
function(i){for(i=0;++i<101;)console.log((i%3?"":"fizz")+(i%5?i%3?i:"":"buzz"))}
That's 80 bytes including the function signature.
Using the falsiness of
''
and||
to provide a fallback output when the number is not fizzy buzzy saves 3 bytes, bringing us down to 64 bytes i.e. 77 bytes including the function signature:function(i){for(i=0;i++<100;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)}