Unexpected token else


#1

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

I continually get the error "Unexpected token else" and I'm unsure of what else to do.
Any suggestions?


#2

Hi you forgot to open the if statement with {


#3

Greetings.
I've added the opening { to the if statement and it still gives me the same error.


#4

Can you post all your code


#5

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
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"
}
};


#6

Well you should add a } at the end for closing compare function
And you should also close all your else if statement

else if (condition) {
    if (condition){

   }
   else {

   }
}

#7

I have rewritten the code the same way you suggested and i'm still getting the same error.


#8

Here how your code should be

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!"
} 
else if(choice1 === "rock") {
if(choice2 === "scissors") {  <=== add this one 
return "rock wins";
}
else {
return "paper wins";
}
}<== add this one to close the else if statement 
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins"
}
else {
return "scissors wins"
}
};
}  <== this one to close the compare function