What if Choice 1 is paper? Syntax error - unexpected keyword 'else'


#1

Maybe you can help me out?

NO PROBLEMS WITH THE CODE BELOW:

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

CAN'T FIGURE OUT WHAT'S WRONG WITH THE CODE BELOW WHEN THE CHOICE 1 IS PAPER.

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

THANK YOU MUCH!


#2

#3

Remember the syntax:

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

you are missing curly brackets for if
look at the above syntax and apply in your code


#4

Thank you so much for the reply!

I have applied curly brackets. Is giving me same syntax error.

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


#5

missing opening curly bracket, and another closing curly bracket for the first else if


#6

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