SyntaxError: missing before statement


#1

I'm getting a syntax error on step 4 and i can't figure out why. Thoughts? Thank you!

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


#2

You should change the else if statement to

else if (0.34 <= computerChoice <= 0.66)

otherwise if the computerChoice is under 0.33, it will simultaneously choose rock and paper!

Btw. you should try using the < operator instead of the <= (it can cause some confusion in later exercises)


#3

Thanks for your help, I made those adjustments, but I'm still getting the same syntax error.


#4

Hi @lcj1188,

If it may help please see the following code example below.
In short you were under attack by the coding gremlins :wink:, there was one or two syntax errors that needed a little fixing and if you apply them you should be good to go.
We removed the unnecessary curly braces after your variable declaration for computerChoice (and the closing brace at the end), we added a semi-colon on the same variable, to tidy it up. Threw in some zero's for good measure on your conditional checks and you should be set to lose lots of games to random chance lol.

var userChoice = prompt("Do you choose rock, paper, or scissors?");
var computerChoice = Math.random(); //{ remove curly brace add semi-colon

    if (computerChoice <= 0.33) { //added 0
        computerChoice = "rock";
    }
    else if (computerChoice <= 0.66) { //added 0
        computerChoice = "paper";
    }
    else {
        computerChoice = "scissors";
    }
    
    console.log(computerChoice);
//} remove curly brace

Hope that helps & a great job coding =)

Cheers,
Michael


#5

Hi @repsaj2000

Just a minor note =)
The example you have supplied is actually incorrect and will not give out the correct result when a value between 0.34 and 0.66 is generated by the random method, rather than a long winded post on why and derail this original post, please feel free to open a new question related to this if you would like an explanation.
Using your idea you have supplied you could write the following out

else if (computerChoice >= 0.34 && computerChoice <= 0.66)

but the example originally supplied by lcj1188 will work just as well, once the little gremlins are beaten off.

Happy coding!

Cheers,
Michael


#6

Thank you so much @mike_in_training! I am good to go now ! :smile: