DEV Community


Discussion on: Unconditional Challenge: FizzBuzz without `if`

jpantunes profile image
JP Antunes

How about with for loops?

const fizzBuzz = n => {
    let x = 1;
    const range = [...Array(n)].map(_ => x++);
    for (let i = 2; i <= n; i += 3) range[i] = 'Fizz';
    for (let i = 4; i <= n; i += 5) range[i] = 'Buzz';
    for (let i = 14; i <= n; i += 15) range[i] = 'FizzBuzz';
    return range.toString();
Thread Thread
kallmanation profile image
Nathan Kallman Author

Nice! I'll accept it even though I said no for loops because I like that you used the stepping statement to simply go through the multiples of 3,5,15. That's creative!

I was more expecting someone to abuse the i <= n into the n % 3 type of check in a traditional fizz buzz. (especially abusing while(n % 3) into a more convoluted if)

Thread Thread
jpantunes profile image
JP Antunes

I missed that! Sorry, but I was running out of ideas :-) Looking forward to seeing your solution(s)!

Forem Open with the Forem app