-2

I'm getting the error "SyntaxError: Unexpected token {" and i don't know why, since everythin seems pretty right ! Could you help me ?

var userChoice = prompt("Choose: rock, paper or scissors?");

var computerChoice = Math.random();

if(computerChoice >= 0 && computerChoice <= 0.33){
    computerChoice === "rock";
} 

else if (computerChoice >= 0.34 && computerChoice <= 0.66){
    computerChoice === "paper";
} 

else (computerChoice >= 0.67 && computerChoice <= 1){
    computerChoice === "scissors";
}
Gabriel Ozzy
  • 157
  • 2
  • 7

2 Answers2

0

Your else block has a condition. That doesn't make sense. An else block is supposed to catch all other situations, so there's no need for a condition. That's why it expects an opening brace.

Either eliminate the condition or convert it to an else if block.

else {
    computerChoice === "scissors";
}

Or

 else if (computerChoice >= 0.67 && computerChoice <= 1){
    computerChoice === "scissors";
}

Also, you should use = for assignment. === is a comparison operator.

computerChoice = "scissors";

Also, computerChoice is first assigned a number, then later a string. That's risky. JavaScript allows it because it's a dynamic language, but it might be better to use different variables to avoid confusion. So finally, we have:

var userChoice = prompt("Choose: rock, paper or scissors?");
var computerChoice = "not selected";    
var rand = Math.random();

if(rand >= 0 && rand<= 0.33){
    computerChoice = "rock";
} 

else if (rand >= 0.34 && rand <= 0.66){
    computerChoice = "paper";
} 

else {
    computerChoice = "scissors";
}
Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
0

Yes, it is syntax error.
else run whenever all evaluation is falsey. So else does not accept condition.
If you want the program to evaluate more condition use else if

if(computerChoice >= 0 && computerChoice <= 0.33){
    computerChoice === "rock";
} 

else if (computerChoice >= 0.34 && computerChoice <= 0.66){
    computerChoice === "paper";
} 

else (){
    computerChoice === "scissors";
}
vdj4y
  • 2,649
  • 2
  • 21
  • 31