DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

1

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
👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay