3... And the good!


#1

Hi guys,

What is the problem?

for (var numbers = 1; numbers <= 20; numbers ++)

if (numbers % 3 === 0) {
console.log("Fizz");
} else if (numbers % 5 === 0) {
console.log("Buzz");
} else if (numbers % 3 === 0 && numbers % 5 === 0) {
console.log("FizzBuzz");
} else {
console.log(numbers);
}

Error: Oops, try again. You printed Fizz when you should have printed FizzBuzz


#2

Just saw this question in another thread, too.

You need to have the first IF evaluation be the &&. So it needs to read:
IF (numbers % 3 === 0 && numbers % 5 === 0) {console.log("FizzBuzz");}

Then the next else/if evaluations don't matter which order. This is because when the first IF evaluation returns true, it no longer needs to evaluate the "else" evaluations.


#3

Got it! It works! Thanks! :smile:


#4

This worked for me:
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);
}