DEV Community

Discussion on: JavaScript FizzBuzz solution in details

Collapse
 
tejaswipandava profile image
tejaswipandava • Edited

I still like the first solution you have provided only change I would do is remove the math reminder operation out of the if loop like below (this improves performance by 3x)

function fizzBuzz(n) {
    let x = 0;
    let y = 0;
for (let i = 1; i <= 100; i++) {
        var result = "";
        x = i%3;
        y = i%5;
        if (x === 0 && y === 0) result += "fizzbuzz";  
        else if (x === 0) result += "fizz";        
        else if (y === 0) result += "buzz";
        console.log(result || i);
    }
}

fizzBuzz(15);

even though the one-liner is fancy it's very complicated to understand and its really slow.