Skip to content

Instantly share code, notes, and snippets.

@aaronsnoswell
Forked from 140bytes/LICENSE.txt
Created September 4, 2011 15:27
Show Gist options
  • Save aaronsnoswell/1193014 to your computer and use it in GitHub Desktop.
Save aaronsnoswell/1193014 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

Fizz Buzz

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 :)

function() {
for(i=0;++i<101;)
console.log(
(i%3 ? "" : "fizz") + // If 3 doesn't divide i, print nothing. If it does, print "fizz"
(i%5 ? i%3 ? i: "" : "buzz" ) // If 5 doesn't divide i, print i or nothing, depending on if 3 also divides i
// Otherwise, print "buzz"
)
}
function(){for(i=0;++i<101;)console.log((i%3?"":"fizz")+(i%5?i%3?i:"":"buzz"))}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Aaron Snoswell http://elucidatedbinary.com
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
<!DOCTYPE html>
<title>Fizz Buzz in 67 bytes</title>
<div>Check the console for the output</div>
<script>
var myFunction = function(){for(i=0;++i<101;)console.log((i%3?"":"fizz")+(i%5?i%3?i:"":"buzz"))}
myFunction()
</script>
@p01
Copy link

p01 commented Sep 4, 2011

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)}

@aaronsnoswell
Copy link
Author

This is my first 140byt.es entry - I wasn't aware the function definition was included in the byte count sorry. And the function only leaks to the global scope through the application of a closure for the variable 'i' - surely that doesn't count? Nicely done lopping it down to 77 :)

@p01
Copy link

p01 commented Sep 6, 2011

No problem.
Yes it does count. If you don't want to leak, use a var statement, or abuse an argumentof the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment