?8 syntax error has buffaloed me. Where is the error or errors of my ways?


#1

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

};
console.log(compare(userChoice,computerChoice));


#2

if the syntax error of the website is not giving you enough information, copy your code into the console, it'll most likely tell you more specific errors, such as where there is an unexpected token in a syntax error.

Press f12, and along the top row of tabs, go to console, or click the window and press esc and a mini console will show up. Paste your code into this area and press enter.

The specifics of your syntax error are 'Unexpected token else'

This means you have a syntax error leading to an unexpected else statement. This most likely due to a missing curly bracket, or missing a semicolon.


#3

Thank-you for the suggestion. I did paste it into the console. Got the same error message. Unfortunately I need a more specific answer. I would welcome any more ideas.


#4

Hey, can you tell me what the error says? I had a problem where I called the compare function but there was a syntax error that said: Syntax error: unexpected else
I solved it, and now its working....maybe I can help.


#5

The error message states: syntax error: unexpected token else.


#6

You closed your function too early, so your else if statement is unexpected and outside your function. The syntax error is on line 28, 'unexpected else statement'.

Just read through your code and make sure you have matching opening and closing brackets, semicolons where they belong, and all other syntax corrections you may need to make.


#7

Line 28 just to make sure we are looking at the same line states" if(choice2 === "rock") {
and you say the curly bracket at the end is closing it too soon.


#8

Line 28 according to the console, else if is unexpected, meaning your code before that point is incorrect. It's a syntax issue, so just look above there and make sure your brackets line up, and you aren't missing any semicolons or you don't have any open strings.


#9

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

help am stuck here


#10

keeps telling me syntax error


#11

semicolons do not go between conditions of an if statement

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

#12

Hey I guess I found your mistake.

1) In the first compare part, the first ever curly bracket that you open { must end at the very last, that is just before you call the function.
So you have to delete that extra curly bracket in the first part where you declare choice1 = rock, (look at the end of this part, you have three }'s delete the last one and also do not put a ; after that)

2) In the second part where you declare choice1 = paper, notice that you have a total of 7 curly brackets, there is one extra closing curly bracket } , delete it.

The same goes where you declare choice1 = scissors,

Also note that you declared choice1 = scissors twice!!

I hope I could help and make it clear to you.

hope it works out!!
have fun coding!!