Skip to content

Instantly share code, notes, and snippets.

@ungoldman
Created August 3, 2012 20:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ungoldman/3251385 to your computer and use it in GitHub Desktop.
Save ungoldman/3251385 to your computer and use it in GitHub Desktop.
Quest for the Shortest FizzBuzz
for (var i=1;i<=100;i++){
var s = '';
if (i % 3 === 0) s += 'Fizz';
if (i % 5 === 0) s += 'Buzz';
console.log(s ? s : i);
}
@ungoldman
Copy link
Author

for (var i=1;i<=20;i++) { console.log(i% 3 === 0 ? i % 5 === 0 ? 'FizzBuzz' : 'Fizz' : i % 5 === 0 ? 'Buzz' : i) }

@ungoldman
Copy link
Author

for (var i=1;i<=20;i++) { console.log(i % 3 ? i % 5 ? i : 'Buzz' : i % 5 ? 'Fizz' : 'FizzBuzz') }

@benabbottnz
Copy link

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

@zivc
Copy link

zivc commented Aug 3, 2015

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

One character shorter!

@LeonardKoch
Copy link

for(i=0;i++<100)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)
A little shorter yet! :)
(Sorry for gravedigging.)

@zivc
Copy link

zivc commented Aug 9, 2017

@LeonardKoch something wrong with your for there m8

Copy link

ghost commented May 27, 2018

console.log(Array(100).fill(0).map((e,i)=>i%3==0?'Fizz':''+i%5==0?'Buzz':''||i).join(''))

It's the shortest I could create... I don't like for-loops.

@micky2be
Copy link

micky2be commented Dec 3, 2018

I can't do shorter than this:

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

@MoritzLoewenstein
Copy link

MoritzLoewenstein commented May 28, 2020

Not the shortest, but certainly an interesting solution i found:
for(i=0;i++<100;)console.log([i,"fizz","buzz","fizzbuzz"][+!(i%3)+ +!(i%5)*2])

@edo9k
Copy link

edo9k commented Jan 9, 2021

I got to 78 characters. But it's not canonical, since it starts from zero, and the result is an array instead of just printing. I'll keep thinking.

console.log([...Array(100).keys()].map(n=>(n%3?'':'Fizz')+(n%5?'':'Buzz')||n))

@edo9k
Copy link

edo9k commented Jan 10, 2021

Got it down to 73 characters, using recursion to iterate over the 1..100 range.

f=n=>{console.log((n%3?'':'Fizz')+(n%5?'':'Buzz')||n);n<100&&f(n+1)}
f(1)

@ludwigfriborg
Copy link

I took a shot at it, got it to 69 chars by only using recursion and string concatenation.

f=n=>console.log(n?n+(n%3?"":"fizz")+(n%5?"":"buzz")+(f(n-1)??""):"")

@Sid110307
Copy link

I got it to 58 chars:

for(x=0;x++<100;)print((x%3?"":"Fizz")+(x%5?"":"Buzz")||x)

@SiddharthShyniben
Copy link

56 ;)

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

@ibrokemycomputer
Copy link

@SiddharthShyniben You gotta use console.log for the challenge, however even with replacing alert you still are the shortest I've seen at 62 characters!!! 🎉

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