Please help! What if choice 1 is paper?- SyntaxError: Unexpected token else


#1

var compare = function (choice1, choice2)
{
if (choice1 === choice2)
{ return "the result is a tie";}

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

SyntaxError: Unexpected token else


Replace this line with your code.


#2

your else should be inside of else if . you closed else if too early

else if (choice1 === "rock") 
{ if (choice2 === "scissor") 
{ return "rock wins";}
} // dont close else if here
else { 
return "paper wins";
}

you should close the function at the end


#3

First of all thank you for taking the time to help.

I am a bit confused. I've attempted to close the function at the end and am still getting the same error code.


#4

can you please post your update code?


#5

else if (choice 1 === "rock")
{
if (choice2 === "scissor")
{
return" rock wins"; }

else {return "paper wins";
}

else if (choice 1 === "paper")
{
if (choice2 === "rock")
{ return "paper wins"; }
else {
return "scissor wins";}
};


#6

else if (choice 1 === "rock")
{ //openig else if
if (choice2 === "scissor")
{ //opening if
return" rock wins";
 } //closing if

else { //opening else
return "paper wins";
} // closing else

did you close else if here?


#7

the first else if was left open
then began my second else if statement

I finished the code by closing 2 else if at the end


#8

you shouldn't do that.

you should have

else if (condition){ //opening else if
  if (condition){ //opening if
    // do something
  } //closing if
else { //opening else
  // do something
} //closing else
} //closing else if

now closing all of your else if statement you should close the function.

one thing you should notice

else if (choice 1 === "rock")

the space between choice and 1.you shouldn't have


#9

when transitioning from the first else if statement to the second else if statement
do i close the previouse else if with a semicolon before beginning the second stement?


#10

no,don't do that

you can read this (Your Guide to Semicolons in JavaScript)


#11

Can u help me?

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

Oops, try again. Your code returned 'rock wins' instead of 'scissors wins' when the inputs are paper and scissors


#12

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