Rock, paper, scissors 4, part 2, so stuck, unexpected token


#1

not sure what i'm doing wrong, but it keeps telling me i have an "unexpected token". any help would be great!

var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice >= .33) {
    computerChoice = "rock";
} else if (.34 <= computerChoice <= .66) {
        computerChoice = "paper";
} else (.67 <= computerChoice < 1) {
        computerChoice = "scissors";
};

thanks!


#2

Hi try to write your number like that 0.33 and 0.66 and your else statement dont need a condition you should write it like that

else {
    computerChoice = "scissors";
}

#3

that totally worked. thank you!

but why did taking the condition off the else statement resolve the "unexpected token {" issue? clearly, i'm still struggling with syntax.


#4

Well to explain this you need to dig a bit deeper. The truth is that the {} after conditions are not mandatory. So what happens when you omit them is that the conditions only work on the very next statement. So basically what the {} do is they wrap as many statements as you like into 1 statement (which is the one next statement).

Now else is the default case and as it handles everything that is not handled by ifs or else ifs there is really no need in a condition for else. What happens is that:

(.67 <= computerChoice < 1)

is not treated as a condition but as the code that is executed when the else case is chosen. The problem is now that you have 2 statement one one line without separator

  1. (.67 <= computerChoice < 1)
  2. {

Because as said the {} wraps it's interior to one statement. Hope that was not too confusing.

And about this condition:
(.34 <= computerChoice <= .66)
Well it doesn't work as expected. It's just evaluated from left to right:

(.34 <= computerChoice) <= .66
(true/false) <= 0.66 // as true ==1 and false == 0
false/true

so if you follow the chain you see that it does the opposite of what it is supposed to do. So as @wizmarco already mentioned better use just <= 0.33 aso.


#5

btw, quick notification!

the > you used in

is the wrong way around right? in this statement it will say everything above 0.33 is rock

counts for the < in if (.34 <= computerChoice aswell!
*if i am not mistaken :stuck_out_tongue:
greetings


#6

good eye, i totally wrote >, instead of <


#7

you have an error in the code
"} else if (.34 >= computerChoice <= .66) {" wrong <=