3/33 SyntaxError: Unexpected token else


#1

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

I've tried a few different things and looked at others code and still can't figure out why this won't work.
Thanks in advance.


#2

No functions after an else statement. SOOOOO

Should change that to

else{ :smiley:}

(Don't really put the smiley face in there :smiley: )


#3

So I tried
else (i % 3 !== 0 && i % 5 !== 0){
console.log(i);
};

but that didn't work either.
Now I'm trying this and still getting "unexpected token else"

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


#4

No semi colons in a connected statement. Like a do/while statement, or an if/else statement:


#5

for(i = 1; i < 21; i++) {
( i % 3 === 0 && i % 5 === 0 ) ? console.log("FizzBuzz") :
(i % 3 === 0) ? console.log("Fizz") :
(i % 5 === 0) ? console.log("Buzz") :
console.log(i);
};


#6

for (var 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)}}

this worked for me


#7

first:

if(condition) {
    // Do one thing here
}
else if (condition) {
    // Do another thing here
} 
else  {
    // Do another thing here
}

than NO semicolon after } inside the if statement.
than you made a mess with fizz and buzz...

else if(i % 3 === 0){
console.log("should be Fizz not Buzz");

#8

Your code was the most helpful for me figuring this out, although it still took awhile for me to debug,
Here's what worked for me,

for (var 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)}
};

Thanks everyone!


#9

Why won't this work? Why do I need the "(i % 3 == 0) && (i % 5 == 0)" first in the if statement anyway?

Here is my code:

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

}


#10

This code worked for me. only the console.log() should have a semi colon after. Delete the semi colons after each else/else if.

for ( var 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);
}
}