Syntax error


#1

I'm new to the whole if statement within an if statement thing, and I don't know what to do. Help would be much appreciated. Here's 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" ;
}
}
}
if (choice1 === "paper") {
if (choice2 === "rock") return "paper wins;"
} else {
return "scissors wins"
}


#2

else if(choice1 ==="rock") {
  if(choice2 === "scissors") {
    return "rock wins" ;
  }
  else {
    return "paper wins" ;
  }   //close your else
}  //close your else if
}   // this one is too much
if (choice1 === "paper") {   // you re supposed to start an new else if
if (choice2 === "rock") return "paper wins;"   //and in that else if, same that above, if and else statment
} else {
return "scissors wins"
}
// here you need to close your second else if ( } )
// and here to close your function ( }; )

Happy coding !


#3

don't give up (and don't forget to call your function => compare(userChoice, computerChoice); ) !


#4

Thank you for your help! I followed your instructions as best as I understood them, but now the console says my code has a parser error. Here's my new code, if you don't mind taking another look:

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


#5

else if(choice1 ==="rock") {
if(choice2 === "scissors") {
return "rock wins" ;
}
else {
return "paper wins" ;
}
}
if (choice1 === "paper") {               // just change this for an else if statment and you should be good to go :wink:
if (choice2 === "rock") return "paper wins;"
} else {
return "scissors wins"
}
}
};


#6

my code now looks like this: and the console says "unexpected keyword 'else' "

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


#7

You change it on the wrong spot ^^'
your structure needs to look like :

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

So like this :

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