DEV Community

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

Collapse
 
nacasha profile image
Izal Fathoni

My approach is to take advantage of string replace 🤣️

const n = 15;

for (let i = 1; i <= n; i++) {
  const fizz = ['Fizz'][i % 3];
  const buzz = ['Buzz'][i % 5];

  const value = `${fizz}${buzz}`
    .replace('undefinedundefined', i)
    .replace('undefined', '');

  console.log(value);
}
Collapse
 
nombrekeff profile image
Keff

Smart!