I am new to programming, and I am currently doing the FizzBuzz test, it looks simple at first but we got some requirements to perform it:
- I can use only one
if
. No multiple branches, ternary operators orelse
. - Unit tests.
I made it by using switch statements, but looking on the internet, I found this way which is shorter, but it is not clear how this process of solving the FizzBuzz challenge is.
This is the code:
var i, values = [, , 'fizz', , 'buzz', 'fizz', , , 'fizz', 'buzz', , 'fizz', , , 'fizzbuzz'];
for (i = 0; i < 100; console.log(values[i++ % 15] || i));
If anyone understands this way of solving the FizzBuzz challenge I would appreciate if it can be explained.