DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Fizz-Buzz

var fizzBuzz = function (n) {
  let arrayResult = [];
  for (let i = 1; i <= n; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      arrayResult.push("FizzBuzz");
    } else if (i % 3 === 0) {
      arrayResult.push("Fizz");
    } else if (i % 5 === 0) {
      arrayResult.push("Buzz");
    } else {
      arrayResult.push(i);
    }
  }
  return arrayResult;
};

console.log(fizzBuzz(5));

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
alexmustiere profile image
Alex Mustiere • Edited

Another implementation

const fizzBuzz = (n) => {
  let arrayResult = [];
  for (let i = 1; i <= n; i++) {
    let r = '';
    if (i % 3 === 0) {
      r += 'Fizz';
    }
    if (i % 5 === 0) {
      r += 'Buzz';
    } 
    arrayResult.push(r || i);
  }
  return arrayResult;
};
Enter fullscreen mode Exit fullscreen mode