7. Syntax error


#1


I am having trouble seeing the syntax error in this code, could someone please take a look and give me a heads up.

Thanks.

var compare = function (choice1, choice2){
    if (choice1 === choice2){
    return "The result is a tie!";
    }
    else if (choice1 === "rock"){
        if (choice2 === "scissors")
        return "rock wins";
    }
        else { 
        return "paper wins"
    }
    else if (choice1 === "paper"){
        if (choice2 === "rock")
        return "paper wins";
    }
    else {
        return "scissors wins";
    }
};


#2

I will give you a hint by marking a part of your code:

Do you see it now?


#3

No unfortunately I don't see it.

Is it an error with the { } ?


#4

Here you forgot to start your opening bracket of if statement

else if (choice1 === "rock"){
        if (choice2 === "scissors")      <<-- Opening bracket here
        return "rock wins";
    }

Another way you can attack this , is that you can simply have no brackets if there is only one line of code but it is not recommended to do so, as it is vulnerable for errors...

if (true)
    console.log("Hello World")

#5

Even just a hint would be appreciated.


#6

I just anwsered your question in post 3. @koda82


#7

like this?


else if (choice1 === "rock"){ if (choice2 === "scissors");{ return "rock wins";
`


#8

Thanks I will keep looking at it to see if I can get it.


#9

No that is wrong Two things first

else if (choice1 === "rock") {
    if (choice2 === "scissors"); {
        return "rock wins";

Dont use the one line method i presented just add the bracket i told you above. Second that semicolon in front of the if statement conditon will also cause an error. It is not needed...

This was your earlier code... Look at my note provided in the code

else if (choice1 === "rock"){
        if (choice2 === "scissors")      <<-- Opening bracket here
        return "rock wins";
    }

#10

Ok you obviosuly dont understand about if/else statements well enough.. When you open a bracket in an if or else statement you must end it when your conditon ends, and start a bracket after your conditon ... So in a if statement this is a example:

if(1==1){ <<-- Notice i started the conditon with a bracket
console.log("Hello There..")
} <<-- Notice i ended it right before the conditon ended

Now in your code example it seems you forget to end your brackets ima give you a few examples where you do that from post #1:


#11

that would be the closing for else.

In addition there is an error that I made on a earlier exercise (5. Both choices are the same) CA has not picked up on this and it is now causing trouble. So I am going back and redoing the exercises that it has effected.

I also see what you are saying about the opening and closing { }

many thanks.


#12

Thanks again for the help I went back over what was going on and was able to look at things with greater clarity.

Helped a lot :grin: