syntax error can't pinpoint the bug in javascript

589 views Asked by At

I continue to receive a syntax error and can't figure out why, please help.

alert ("CAN YOU BEAT VALERIE AT ROCK PAPER SCISSORS?");
var userChoise = prompt ("Rock, Paper, Scissors");

var computerChoice = Math.random();

if (computerChoice < 0.34) {
computerChoice = "rock";
}

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

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

console.log("Valerie Dam picks" + " " + computerChoice);

Chrome Console throws the following syntax error:

Uncaught SyntaxError: Unexpected token {
at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
2

There are 2 answers

1
chris97ong On BEST ANSWER
  1. 0.34 >= computerChoice < 0.67 is not valid in JavaScript. Use something like computerChoice >= 0.34 && computerChoice < 0.67 instead.

  2. the last block of else [else (0.67 >= computerChoice <= 1)...] should be else if.

So your corrected code should be like this:

alert ("CAN YOU BEAT VALERIE AT ROCK PAPER SCISSORS?");
var userChoise = prompt ("Rock, Paper, Scissors");

var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
}

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

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

console.log("Valerie Dam picks" + " " + computerChoice);

Working Fiddle

2
Claudio Redi On

This structure doesn't exist in javascript

0.34 >= computerChoice < 0.67

It's not possible to express a range like that. You need to replace it by

computerChoice >= 0.34 && computerChoice < 0.67

The same applies for

0.67 >= computerChoice <= 1