...And the good! / FizzBuzz Javascript


#1


https://www.codecademy.com/en/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#


SyntaxError: expected expression, got keyword 'else'


function fizzBuzz () {
    for (i = 1; i < 21; i++) {
        if (fizzBuzz % 3 === 0) {
            console.log("Fizz");
        }
    } else if {
        if (fizzBuzz % 5 === 0) {
            console.log("Buzz");
        }
    } else if {
        if (fizzBuzz % 3 && fizzBuzz % 5 === 0) {
            console.log("FizzBuzz");
        } else {
            console.log(fizzBuzz);
        }
    }
};
I just keep getting an error even when I put the else if / else statement on the second to last closing curly brace.


#2

First of all you need to declare "i" as a variable (var) :slight_smile:

When using the "else if" statement, you dont need need to declare another "if" statement under. And it would be the variable "i" which need to be divisible, not "fizzbuzz".
Like this:

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);

hope this helps! :sunglasses:


#3

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);
}

}


#4

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