4/9 SyntaxError: expected expression, got keyword 'else'


#1

So I've been writing the code for the rock, paper, and scissors game, but I keep getting this error: SyntaxError: expected expression, got keyword 'else'. I've gotten this error many times on the previous lessons, and I would delete some things and it would work without me knowing what fixed it. I'm hoping someone can point out my mistake here and help me. :smile:


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

var computerChoice = Math.random(0,1)

//computer chooses rock, paper, or scissors

if (computerChoice <= 0.33) {

computerChoice === "rock";

};

else if (computerChoice <= 0.66) {

computerChoice === "paper";

};

else (computerChoice <= 1) {

computerChoice === "scissors";

};

console.log(computerChoice)


#2

In if/else statements or if/else if/ else statements, you must NOT put semi colons after the if statement, then start the else statement. Why? Because what a semi colon does is that it ends, or is like a disconnecting thing.

Look closely at your if, else if statements.


#3

You don't have to use math.random(0,1) since it does give ths range.
Also remove the === inside the if statements. You want to assign something to computerChoice, you just compare computerChoice with rock,paper,scissors.


#4

Here is a link about the problem with the semicolon.

You don't have to use math.random(0,1) since it does give ths range.

I guess this means that you don't need the 0,1 as parameters as this is already the default range and you cannot adjust this by using parameters. Also use Math with an upper case M. Anyway good answers @eranor @ragezapper

and last but not least do not use a condition after else as else is the default case if no other condition fits.


#5

thankyou for this been stuck for days! have managed to solve my problem with this bit of info :slightly_smiling: