Skip to content

Instantly share code, notes, and snippets.

@audunolsen
Last active August 10, 2018 21:16
Show Gist options
  • Save audunolsen/b51807c0480dcdaeb1f87d6225fd6c33 to your computer and use it in GitHub Desktop.
Save audunolsen/b51807c0480dcdaeb1f87d6225fd6c33 to your computer and use it in GitHub Desktop.
My blind attempt at a compact script for the FizzBuzz game.
for(i=1;i<101;i++)
o =i%3?'':'Fizz',
o+=i%5?'':'Buzz',
console.log(o||i)
@audunolsen
Copy link
Author

78 bytes if written on one line: for(i=1;i<101;i++){o=!(i%3)?'Fizz':'';o+=!(i%5)?'Buzz':'';console.log(o?o:i);}

@EirikRS
Copy link

EirikRS commented Jan 22, 2018

nice one!

@audunolsen
Copy link
Author

Update! Now 77 bytes—one saved!
for(i=1;i<101;i++){o=!(i%3)?'Fizz':'';o+=!(i%5)?'Buzz':'';console.log(o||i);}

@audunolsen
Copy link
Author

audunolsen commented Jan 28, 2018

Update! Now only 69 bytes on one line!!
for(i=1;i<101;i++)o=i%3?'':'Fizz',o+=i%5?'':'Buzz',console.log(o||i);

Don't know why it took me so long to realize that I could flip the true/false values associated with the ternary operators, allowing me to omit the exclamation mark and the parenthesis: o=i%3?'':'Fizz' == o=!(i%3)?'Fizz':''

Swapping ; for , also made {} redundant because it's now only one program statement.

@audunolsen
Copy link
Author

audunolsen commented Aug 10, 2018

Update… I recently did some googling to see if I could find anything shorter, and yeah, there is. A Medium Article titled Breaking Down The Shortest Possible FizzBuzz Answer examines the following code:

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

This actually looks a lot like my blind attempt, but it does a couple of smart things;
Remove i++ from the for-compound, and instead increment i before the first ternary-if, saving a single "i".
The other (fairly obvious) improvement is just omitting the output variable and placing any potential output-logic as a console.log argument. The solution is 64 bytes.

At this point I'm beaten. The room for improvement is so tiny that revisions of my solution simply become a carbon copy of the above line.

Maybe I'll try to find a new radical approach, the fight continues…

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