Wrong Result (...And the Good!, 3)


#1

Hi, We were supposed to print out numbers 1 to 20 and if the number was divisible by 3 print out , 'Fizz". If the number was divisible by 5, print out, 'Buzz' and if it was divisible by both 5 and 3, print out, 'FizzBuzz'.
However, my code keeps on getting the wrong result.

for(i=1; i<21; i++){
if(i % 3 === 0){
console.log("Fizz")
}
else if(i % 5 === 0){
console.log("Buzz")
}
else(i % 3 === 0 && i % 5 === 0)
console.log("FizzBuzz")

}
console.log(i)
Any kind soul please help me! Thank You!


#2

@dragonwizard22,

You will have to use the most-specific condition as =first= condition in your IF ELSE-IF ELSE statement structure.
So replace position of

 (i % 15 === 0)

#3

do you mean:

instead of:

if (i % 3 === 0 && i % 5 === 0){

use:

(i % 15 === 0)

if so, can you elaborate a bit. I'm not getting it. Thank you kindly.


#5