Computer Choice Part 2: Unexpected Token Else


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661


It gives me a syntax error with the following code. The console says it's an "unexpected token else." I don't understand why- I've tried it this way, with the "else if" replaced by "if else", deleted semicolons, added semicolons, and checked curly brackets, and I'm still stuck. Please help!


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


#2

We never write a semi-colon before else. We also do not end an if/else with ;.

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

No semi-colons anywhere except the statements inside each block.


#3

Thanks! I tried it this way and it worked first time!

I don't know why I thought I needed semi-colons...


#4

var userChoice= prompt("Do you choose rock, paper, or scissors")
var computerChoice = Math.random()

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

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

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

I saw you helped OP out a little with his syntax. I've tried to look at other posts and haven't been able to fix it myself. Is this a problem like OP with semi-colons in the wrong places?


#5

The problem is with the condition expressions.

if (computerChoice <= 0.33) {

}
else if (computerChoice < 0.66) {

}
else {
    computerChoice = "scissors";
}

Write the condition on the same line as the keyword, and there is only one condition expression () permitted in if ().


#6

var userChoice= prompt("Do you choose rock, paper, or scissors")
var computerChoice = Math.random(){

if
(computerChoice<=0.33) {computerChoice==="rock";}
else if
(computerChoice<0.66){computerChoice==="paper";}
else {computerChoice==="scissors"};

is there any way you could expand on that. I think i'm getting a little bit closer but there is just something i'm not catching here. Thanks


#7

As mentioned above, the expression should be on the same line as the keyword. The following is valid...

if (condition)
{

}
else if (other_condition)
{

}
else
{

}

and so is this,

if (condition) {

}
else if (other_condition) {

}
else {

}

Curly braces are okay at the beginning of a line, but parentheses should have a keyword in front to give them meaning and prevent accidental separation.

Let the reader, including yourself, see the curly braces, Don't bury them in the code. Style guides recommend that at least the closing braces should have their own line. White space is free and saves a tonne of bother during debugging.

if (computerChoice <= 0.33) {
    computerChoice = "rock";
} 
else if (computerChoice < 0.67) {
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}

Note the assignment operator, = is used to make assignments. === is for making comparisons. Also recommended in style guides, surround operators with white space so they can be easily seen.


#8

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