FizzBuzz
Write a program that prints 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:
test % 3 === 0 return Fizz
test % 5 === return Buzz
test % 3 === 0 AND test % 5 === 0 return FizzBuzz
Should return true
##
function fizzBuzz(n) {
for(let i = 1; i <= n;i++) {
// is the number a multiple of 3 and 5
if(i % 3 === 0 && i % 5 === 0) {
console.log('fizzBuzz')
}
else if (i % 3 === 0 ) {
// is the number a multiple of 3
console.log('fizz')
}
else if(i % 5 === 0) {
console.log('buzz')
}
else {
console.log(i)
}
}
}
-
First we make a for loop where we start from 1 and increment until given parameter and increment by 1 every step.
-
We check if i module 3 is strictly equal to 0 and module 5 is strictly equal to 0. If yes then we print FizzBuzz.
-
We have two more else if, first is checking if i module 3 is equal to 0. If yes we print Fizz.
-
Second else if we check if i module 5 is equal to 0. If yes then we print Buzz.
-
if NONE of the numbers looped work we return with else case the number i