7. What if choice1 is paper?


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661#

Hi all. I am stuck on the "What if choice1 is paper?" on making the rock paper scissors game. I dont understand why it keeps coming up with an error message saying: SyntaxError: Unexpected token else. When I checked my code on lab codes workspace, theres a red cross on the start of this section of the code: else if (choice1 === "paper").

Any ideas whats missing? I am pretty sure it has to be a curly bracket but theres too many and it feels like its getting out of control where one starts and when one ends :frowning:


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


I cant seem to get it right!
#2

HI here you should remove one } because it close the compare function earlier and you should close the else if statement

else if(choice1 === "rock")
{
if(choice2 === "scissors"){
        return "rock wins";
    }
} <== remove that one 
else {
    return "paper wins";
}
} <== add one here for closing the else if statement

#3

Thanks for the swift and helpful post wizmarco :slight_smile:
Any advise you can give me to help position curly brackets in the correct place?


#4

for the else if statement maybe that

else if  (condition) {//open the else if statement

    if (considition) {//open the if statement

    }//close the else statement
    else {//open the else statement

    }//close the else statement

}//close the esle if statement

to give four space to your statement insde the else if statement like that it's easier to see if you forgot to close one or you have more than you need.
Hope that could help you .


#5

It does. Thank you :slight_smile:


#6

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

Here is the completed code. It worked for me.