Easy Problems

412. Fizz Buzz

Solutions

function fizzBuzz(n: number): string[] {
  let res = [];
 
  for(let i = 1; i <= n; i++) {
      let dividedBy3 = i % 3 === 0;
      let dividedBy5 = i % 5 === 0;
 
      if(dividedBy5 && dividedBy3) {
          res.push('FizzBuzz')
      } else if(dividedBy3) {
          res.push('Fizz')
      } else if(dividedBy5) {
          res.push('Buzz')
      } else {
          res.push(i.toString())
      }
  }
 
  return res;
};
Last Update: 11:43 - 16 April 2024
Math
String
Simulation

On this page