Don't understand why my code doesn't pass


#1

Hi guys, I might be dumb or something but I can't figure out what is wrong with my code, it seems that it works just fine except for the second "else if"... statement, nowhere on the screen can I see "FizzBuzz", and REALLY don't know why :slightly_smiling: thank for your help,

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


#2

Sorry for the "screwed" indentation, it worked in my code but not here, my bad.


#3

You only write FizzBuzz if you didn't write any of the others (else if), and both of the others will trigger on anything that FizzBuzz would trigger on.


#4

Thanks ionatan, got it, I was indeed dumb :wink:


#5

well, according to your logic, if a number is divisible by 3, it will print Fizz, without going to else if part.
similarly with 5.
put (i % 5 === 0 && i % 3 === 0) above everything and that should work.
for (var i = 1 ; i <= 20 ; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
}
else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}


#6

Thanks alreadytake, I figured it out finally :slightly_smiling: