# 3....And the Good! Printed Fizz when you should have printed FizzBuzz

#1

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

Where is the mistake? Anyone? Thanks

#2

I have the same problem with my code, which is identical to yours..

#3

I figured it out:

The code should be the one below, as you want your program to think like this:

If the number is divisible by 3 AND 5, i will print FizzBuzz.
Else If the number is divisible just by 5, then i will print Buzz.
Else if the number is divisible just by 3, then i will print Fizz.

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

In the code above, by writing the first condition else if (i % 3 === 0, the program only reads that condition and cannot proceed to the following one, which includes the first one too ((i % 3 === 0) && (i % 5 === 0)). Donno if I managed to get you to understand the reason, but try it. It'll work

#4

You're right, thank you!

#5

i had the same error with my code but your solution worked for me:slight_smile:

#6

I have the same error. Thank you!

#7

I had the same problem. Can anybody explain what is going on? The order of "else if's" shouldn't be the problem... Is this a bug or what?

#9

#10

This topic is really old, if you still have a question go here and make a new topic