# My computerChoice output does not correlate to Math.random?

#1

Hi, I'm confused how this is working!

Can anyone tell me why the Math.random number generated does not meet the conditions of the "rock/paper/scissor" output for the computer's choice? For instance, 0.155... returns paper, when it should return rock? Both the number and output randomly change every time I run it, but they never match according to my conditions!!!

Here's my code:

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

if(0 < computerChoice < 0.33) {
console.log(computerChoice);
computerChoice = "rock";
} else if(0.34 < computerChoice < 0.66) {
console.log(computerChoice);
computerChoice = "paper";
} else {
console.log(computerChoice);
computerChoice = "scissors";
} console.log("Computer: " + computerChoice)

#2

``````// rock
0 < 0.155213665 < 0.33
true          < 0.33
false``````

``````// paper
0.34 < 0.155213665 < 0.66
false         < 0.66
true``````

In Python, your comparisons would have worked due to operator chaining. Here, they are just getting evaluated from left to right.

``````Number(true)  // 1
Number(false) // 0``````

#3

Ahh, okay! Yep, I was remembering using that syntax in Python.
Still kind of confused as to why the 0.155 < 0.33 condition in rock would return false, but now I know not to use 'operator chaining' in JS. Thank you!!

#4

A post was split to a new topic: Why is <0.66 sufficient?

#5

@nataliedeclerck As ionatan already explained the statement ist written from left to right so:

``0 < 0.155213665 < 0.33``

boils down to:

`(0 < 0.155213665) < 0.33`

where 0 < 0.155213665 is true. And true has a value of 1 so what's left is

`1 < 0.33`

which is obviously false. So although the syntax is correct the logic is not what one would expect.

#6

OHHHHH. of course because true = 1.
That's what I needed to hear, thank you.