Lesson 7 Syntax Error


#1

Hi I'm stuck on lesson 7. Here's the code:

else if(choice1 === "rock") {

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

It gives me a syntax error. What am I doing wrong? Thanks in advance for any help.


#2

Please post all of your code, and the error message, this doesn't give enough information to show what the problem is.


#3

Oh sorry! Here it is:

/*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 === "rock") {

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

Error is: "SyntaxError: Unexpected keyword 'else'"


#4

If I'm counting correctly, you have an extra curly close bracket in the middle in the group of three (after "paper wins"). This means it thinks the 'if function' has closed off after "paper wins". It then sees the next "else if" as a new function, rather than linked to the original "if" above (hence it telling you "Unexpected keyword 'else'").

I think you'll also have to close an extra set of curly brackets at the end as well.

I think I've put my suggested changes into the below, but it's my first time commenting so I'm not sure how it works.

Let me know if that sorts it!


#5

That worked! Thank you!