Rock paper scissor error unexpected token else - where?


#1

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 win";
}
}


#2

Change these two to else ifs.
The format goes like this:

if() {
}
else if() {
}
else{
}

#3

sorry i didn't understand can you explain please ?


#4

your code is correct what @jibblyj is trying to say is try to make sure there is a closing brace } for every opening brace {


#5

Sure!
if is always first, because that is the start, it's where you go if this happens.. do this. if requires something between brackets after it to tell the code what to look for. Then comes else if, this is used between if and else. else if also requires something in the brackets, to tell it when to start. This is to say: "If the first one isn't true, do this. else always comes last because this is the final resort - it's where the code goes "If nothing else is True, do this.

For example:

if (3 < 2) {
console.log("3 is less than 2!");
}
else if (3 < 1) {
console.log("3 is less than 1!");
}
else {
console.log("3 is greater than 1 and 2!");
}

#6

It isn't correct, it should be if...else if...else


#7

My code is the pretty much the same and I'm getting the same 'Else' error. If i remove the 'else if' and just make it an if statement to evaluate (choice1 === "paper"). I get this error: SyntaxError: Illegal return statement.

My code looks identical to the hint syntax. I have no idea what I'm missing.


#8

Your closing braces are not in the correct place. You also missing a semi colon in the first statement.

Like rydan says, if you have an if statement then make sure you have an opening and closing brackets and then the opening and closing braces. If you look at one of the mistakes you have made...

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

This is what your code should look like...

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

If you can figure out the difference between the one you made and the one I corrected then you should be able to find out the other 2 problems that is wrong with your code


#9

Thanks steve80, I actually checked out another code snippet submitted by a user and was able to amend mine.

Very strange but it seemed to be throwing an error because I had brackets on my embedded functions. I changed my code to reflect below(without additional brackets on my statements)

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


#10

I tried that code above and it didn't work for me although I can't see anything wrong with it. I know why it didn't work and that is because is no opening and closing braces on two of your if statements. On your last section of code...

else if (choice1 === "paper"){
    if (choice2 === "rock") //IF YOU PUT ANOTHER OPENING BRACE HERE
        return "paper wins";
    }
    else{
        return "scissors wins";
    }
}

...and if you put another closing brace at the end of this code (so there would be three closing braces) then the program works for me.


#11

Yea I thought there should braces there too, which is why removing it in order to be able to submit seemed very odd.

I made it to the last lesson, the function(game) works perfectly when I put in a game variable, but the editor is still flashing an error.

Not sure what type of bug may be going on.


#12

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";
}
}
(WHAT IS WRONG IN THIS CODE ?)