Skip to content

Instantly share code, notes, and snippets.

@remy
Created May 8, 2011 18:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remy/961583 to your computer and use it in GitHub Desktop.
Save remy/961583 to your computer and use it in GitHub Desktop.
fizzbuzz - as short as I can - demo: http://goo.gl/Ssyal
i=0;do console.log(++i%15?i%3?i%5?i:'buzz':'fizz':'fizzbuzz');while(i<100)
@remy
Copy link
Author

remy commented May 8, 2011

Example: jsconsole output

Let's see if we can get some code tennis going...

@mathiasbynens
Copy link

Damn, f='fizz',b='buzz',i=1;do console.log(i%15?i%3?i%5?i:b:f:f+b);while(++i<=99) is actually 1 byte longer… (And less readable!)

@remy
Copy link
Author

remy commented May 8, 2011

Heh - yeah, I tried that already - seems daft that I repeat the text, but it's shorter :-P

@mathiasbynens
Copy link

71 bytes (3 bytes shorter than the original), by using while instead of do…while:

f='fizz',b='buzz',i=0;while(++i<101)console.log(i%15?i%3?i%5?i:b:f:f+b)

…which is only 65 chars using alert instead of console.log (as per the original challenge):

f='fizz',b='buzz',i=0;while(++i<101)alert(i%15?i%3?i%5?i:b:f:f+b)

Even shorter (70 bytes):

i=0;while(++i<101)console.log(i%15?i%3?i%5?i:'buzz':'fizz':'fizzbuzz')

…which is only 64 chars using alert instead of console.log:

i=0;while(++i<101)alert(i%15?i%3?i%5?i:'buzz':'fizz':'fizzbuzz')

@mathiasbynens
Copy link

Updated my comment :)

@remy
Copy link
Author

remy commented May 8, 2011

Yours only goes to 99 - but easily fixed: ++i<=100 and you're still 2 characters less.

Passes crown

@rwaldron
Copy link

rwaldron commented May 8, 2011

ok, so this is obviously not the " smallest", but I'm out to lunch and can't really play : / https://gist.github.com/838765

@mathiasbynens
Copy link

@remy: ++i<=100, or ++i<101 which is even shorter :)

@rwaldron
Copy link

rwaldron commented May 8, 2011

while I'm out to lunch... Hehehe... Can someone try this using node repl... Without the console.log()?

@cowboy
Copy link

cowboy commented May 8, 2011

I was out today, but I'm back now. Here's my version, weighing in at 64 characters.

for(i=0;i++<100;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)

@remy
Copy link
Author

remy commented May 8, 2011

I think The Cowboy just hit that one out of the park (note that you want 101 and not 100 - because you're missing the last fizzbuzz - still, you win!)

@cowboy
Copy link

cowboy commented May 8, 2011

My example counts from 1 to 100!

@remy
Copy link
Author

remy commented May 8, 2011

Doh!

@ryanseddon
Copy link

Do I win? No. 63 chars

i=100;while(i--)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)

@garrow
Copy link

garrow commented May 8, 2011

@ryanseddon You get an extra FizzBuzz at 0.

@cowboy
Copy link

cowboy commented May 9, 2011

FWIW, for(init;condition;) will always be one character less than init;while(condition).. but still, I'd imagine that per the rules, you have to count upwards, from 1 to 100.

@MCalligaris
Copy link

62 chars jsconsole output

for(i=0;++i<101;console.log(i%5?f||i:f+'Buzz'))f=i%3?'':'Fizz'

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