3. ...And the good! || Why does my code doesn't run?


#1

I've been trying to understand this for hours.
My code is this:

for(i=1; 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);
        
    };
};

And I keep getting a "SyntaxError: Unexpected token else" message.

I've looked it up in the foruns, and found someone saying that this code works:

for(var i = 1; i < 21; 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);
    }
};

The thing is, I compared both of them and I can't understand why mine doesn't work, while the second one does.
What am I doing wrong?


#2

its the semi colons ; on the closing brackets for if, else if that's causing the error


#3

God, I can't believe it! how frustrating. You're right! It was a ";"
I removed them, and it worked! thank you SO MUCH. I'll try and pay attention to that sort of thing from now on.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.