I can’t figure out what’s wrong?


#1

for (var i = 0; i < 21; i++) {

if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");

} else
if (i % 3 === 0) {
console.log("Fizz");

} else
if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);

}
};


#3

basically you re calling else twice instead of else if. And just to be sure, you should wrap your separate condition in parentheses
if (i % 3 === 0 && i % 5 === 0) => if ((i % 3 === 0) && (i % 5 === 0))


#4

If anything, we should be looking for ways to simplify code, not make it more muddled. Excessive use of brackets just impedes readability and reinforces the idea that we don't have a handle on the logic or how it is evaluated.

If we look at the OP code carefully, we can see clearly the else if statements.

} else
if ( ... ) {
} else
if ( ... ) {

I don't see a problem with this code.


#5

You need to start the iteration process at 1 instead of 0:

for (var i = 1; i < 21; i++)

If you start the process at 0, then you will be outputting too many things onto the console.


#6

Been waiting for someone to point this out.


#11

I have the exact same code except my iteration starts at 1. I get an error message saying the syntax is wrong. I've been searching for the error for half an hour but I can't find it! Did you find out what was wrong?


#13

This is a resolved thread. Please start a new topic with your code included. Thank you.


#15