JS Algorithm – fizzbuzz

Toronto Islands, Toronto, Canada by https://unsplash.com/@schuh

Write a program that console logs the numbers from 1 to n. But for multiples of three print ‘fizz’ instead of the number and for the multiples of five print ‘buzz’. For numbers which are multiples of both three and five print ‘fizzbuzz’.

Example
fizzBuzz(5);

1
2
fizz
4
buzz

Code:

function fizzBuzz(n) {
  // Initialized the counter in 0
  let i = 0;

  // Will execute the code block at least once, before checking if the condition is true
  do {
    i += 1; // Increment one every cycle

    // Is the number multiple of 3 and 5?
    if (i % 3 === 0  && i % 5 === 0) {
      console.log('fizzbuzz');

      // Is the number multiple of 3?
    } else if (i % 3 === 0) {
      console.log('fizz');

      // Is the number multiple of 5?
    } else if (i % 5 === 0) {
      console.log('buzz');
    } else {
      console.log(i);
    }

  } while (i < n); // Condition
}

fizzBuzz(15);

Result:

By Cristina Rojas.