DEV Community

Discussion on: [Challenge] 🐝 FizzBuzz without if/else

Collapse
 
speratus profile image
Andrew Luchuk

Thanks for the fun challenge! My solution uses bitwise operators and objects to get the answer:

function fizzbuzz(n) {
    for (let i = 1; i < n+1; i++) {
        outputs = {
            [i]: i,
            [i+1]: 'fizz',
            [i+2]: 'buzz',
            [i+3]: 'fizzbuzz'
        }
        let fizz = +(i % 3 == 0);
        let buzz = +(i % 5 == 0);
        buzz <<= 1;
        let fizzbuzz = fizz ^ buzz;
        console.log(outputs[i+fizzbuzz]);
    }
}
Collapse
 
nombrekeff profile image
Keff

Neat!