Algorithms and Data Structures – FizzBuzz

--- Directions
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
Hint
Using operator %
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
function fizzBuzz(n) {
    for (let i = 1; i <= n; i++) {
        if(i % 15 == 0) {
            console.log("fizzbuzz")
        } else if (i % 5 == 0) {
            console.log("buzz")
        } else if (i % 3 == 0) {
            console.log("fizz")
        } else {
            console.log(i)
        }
    }
}
cs

Leave a Comment

en_USEnglish