Unexpected token: Else


#1

I'm confused since it says "Unexpected token: Else" What is wrong with my code?

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 think you may have intended this one too many?


#3

I think you should close the curly brackets right after each condition but u have closed bunch of them at the end.


#4

It's hard to read, so I edited your code and found where the problem was:

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") { // this line has error
        if (choice2 === "rock") {
            return "paper wins"
        } else {
            return "scissors wins"
        }
    }
}

As you can see, you missed ** } ** before else if (choice1 === "paper") { // this line has error. Should be:

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") { // missed } before else if()
        if (choice2 === "rock") {
            return "paper wins"
        } else {
            return "scissors wins"
        }
    }
}

So, you have 3rd condition choice1 === "paper" inside 2nd choice1 === "rock" after if-else statement. That's why you have "Unexpected token: Else" because after else you have else if.


#5

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