...And the good! Unexpected token {


#1

Does anyone know whats wrong with the below - the console is returning a syntax error "Unexpected token {"

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

#2

else (numbers[i] % 3 == 0 && numbers[i] % 5 == 0) {
            console.log("FizzBuzz"); 
        }

With if statements, remember that "else" never has any parameter, it's the statement that takes ALL arguments that don't fall into the if statement.

The problem is that if you take out the "else" parameter that you still won't get a correct answer. What you'll need to do is make it an if statement and put it in front of the other if statements. Make sure you change the other if statement:

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

to else if, so that it doesn't go through both if statements:

 else   if (numbers[i] % 3 == 0) {
/*^*/          console.log("Fizz");
        }

#3

I know this is kind of vague, but if you don't understand it just let me know and I'll try to explain it better.


#4

Add else to the first if? That throws up an error, maybe I'm not doing exactly what you mean. Would you be able to clarify?

Tried adding an else to the end of the if statement. Seems to work syntactically, but just displays a long list of undefined:

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

#5

You have your if statements structured wrong. Right now there is no way your code would work It will never print "FizzBuzz"

var numbers = []
for (i = 0; i <= 20; i++) { 
    console.log(numbers[i]);
    if(numbers[i] % 3 == 0 && numbers[i] % 5 == 0) { //<Put FIZZBUZZ first!
        console.log("FizzBuzz"); 
    }
    else if(numbers[i] % 3 == 0) {
        console.log("Fizz");
    }
    else if(numbers[i] % 5 == 0) {
        console.log("Buzz"); 
    }

    else {
        console.log(numbers[i]);
    }
}

else if statements will cancel out if another else if, or if statement in front of it is proven "true". So be careful in what order you put your "if, else-if, else" statements, because that could easily determine if you pass or fail. This is called a Logic Error


#6

Gracias! That makes sense, will not be making that mistake again.


#7

why is it that you only use 2 equal signs? I thought it was 3?


#8

2 or 3 equals sign does the same thing usually.

The only thing is they compare different things

"==" compares only values. 1 == "1" == true

"===" compares values and operator types. true === true | false === false 1 === 1.

This comparison is between Booleans, Strings, Integers, and Arrays. Otherwise comparative operators only work as I have mentioned.