Unexpected token {


#1

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


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 === .34, .66){
    computerChoice = ("Paper");
} else (computerChoice === .67, 1){
    computerChoice = ("Scissors");


#2

Bracket mismatch is throwing the error. But even it that error wasn't thrown, the above would still raise an error. JavaScript doesn't have tuples, and neither does it have range capability.

If we start from the smallest and work up to the largest, we can use two simple if statements:

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

#3

thank you for your quick response!


#4

The first time I read this, I clicked back in anger thinking that you were wrong. Now I understand your clever solution and see that you are right. 'If' statements function like a waterfall, where it only checks the input against the later conditions if the prior conditions have not been met.

I was looking to do this:

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 >=.34 && <=.66) {
computerChoice = "paper";
}
else (computerChoice >=.67 && <=1) {
computerChoice = "scissors";
}

but keep getting "Unexpected token <=". I'm thinking that this is an indicator that the operator && is being used incorrectly or does not exist in Javascript.


#5

Figured out that this was incorrect because there is nothing to specify on an "else" statement since "else" encompasses everything that does not meet the previous conditions. Silly me.