-
-
Save audunolsen/b51807c0480dcdaeb1f87d6225fd6c33 to your computer and use it in GitHub Desktop.
for(i=1;i<101;i++) | |
o =i%3?'':'Fizz', | |
o+=i%5?'':'Buzz', | |
console.log(o||i) |
nice one!
Update! Now 77 bytes—one saved!
for(i=1;i<101;i++){o=!(i%3)?'Fizz':'';o+=!(i%5)?'Buzz':'';console.log(o||i);}
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.
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…
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);}