4. SyntaxError: Unexpected token else


#1


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice <= 0.33) {
console.log("rock")
};
else if (computerChoice <= 0.66) {
console.log("scissors")
};
else {console.log("paper")};



Error message: "SyntaxError: Unexpected token else"


Replace this line with your code.


#2

You have semi-colons in positions that ruin your code. A semi-colon signifies a stopping point in JS. You don't want to stop halfway through deciding what the computer's choice is, do you?


#3

Careful with those semicolons!

if(condition){
//something
} else if (conditionTwo){
//something else
} else {
//something different
};


#4

I didn't even bother checking my semicolons given the nature of the error- was too busy rechecking what was possibly wrong with my last "else" statement. Anyway I got it now, thank you!


#5

Oops yeah, completely neglected that. Thanks!


#6

Hello

I am having trouble with it as well
i was making sure where i am putting the semicolons.
but the problem is i wrote the code correctly but when i type in the number in the prompt it won't accept it. here is what i wrote

var userChoice = prompt("Do you choose rock, paper or scisssors?");

var computerChoice = Math.random();
console.log(computerChoice);

if (computerCoice <= 0.33) {
console.log("rock");

} else if (computerChoice <= 0.66) {
console.log("scissors");

} else {
console.log("paper");

}

can some one help me please?


#7

Two problems


  1. You want to make computerhoice equal to the new value. Here you're just having it log the values
  2. You don't have it set to print or log userChoice, so it wont print whatever you type

#8

ok I see now thanks for you help :slight_smile:


#9

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