Conditions syntax question


The code is working fine, but I have a question. I tried at first to write a condition with two inequalities, as in
(0 < computerChoice <= 0.33)
and did a similar expression in the else if condition.
The code ran but when I ran it a number of times I saw suspicious output. So I rewrote the condition as 2 conditions joined by && as below.
I didn't keep close enough track of the output (I was logging the random number to the console to follow what happened) but it never took the first condition even when the random number was less than .33, and it did not seem to reliably select the correct second or third condition. After rewriting the conditions everything worked fine.

Just want some clarification as to joining two conditions to create a single condition expression.

Replace this line with your code. 

if (0 <= computerChoice && computerChoice <= 0.33) {
    computerChoice = "rock";
 -- etc


I don't think JS ignored the <= 0.33 part (i.e. if it took 0 < computerChoice as true) because then I would have consistently gotten "rock" in the console. I NEVER got "rock", always either "scissors" or "paper". Even when the random number started with .27 I did not get "rock".

For the else if I had tried (0.33 < computerChoice <= 0.66)
For the else I had not bothered (since anything not in the first 2 ranges would have to fall in the third range)

So, although I fixed, it, I don't really understand how it was handling the first version of the script.


Those operators take two arguments and you provided three, where should the fourth one come from? Refer to order of operations to figure out what it ends up doing.


I didn't quite get how the template for the comment works so stuff got cut. Not a problem of order of operations (as a past math grad student, I know that one)--my question was whether you can string two conditions together as in (0.33 < computerChoice <= 0.66) which I assume would read left to right. When I separated it into 2 conditions separated by && it was fine. Just don't know if javascript allows a x < variable < y expression when testing conditions, as opposed to x < variable && variable < y


You did ask that!

Chaining comparisons together would be an exception to normal behaviour which as you said is evaluating from left to right for operators of equal priority.

Python allows chaining comparisons:

>>> 3 < 5 < 7 < 8

But it's just syntax sugar, it still has to be translated into instructions to be carried out:

  2           0 LOAD_CONST               1 (3)
              3 LOAD_CONST               2 (5)
              6 DUP_TOP
              7 ROT_THREE
              8 COMPARE_OP               0 (<)
             11 JUMP_IF_FALSE_OR_POP    34
             14 LOAD_CONST               3 (7)
             17 DUP_TOP
             18 ROT_THREE
             19 COMPARE_OP               0 (<)
             22 JUMP_IF_FALSE_OR_POP    34
             25 LOAD_CONST               4 (8)
             28 COMPARE_OP               0 (<)

(Python is compiled into byte-code before execution, this is a human-readable representation of the bytecode for that expression. What's going on in there is a bunch of juggling with the stack of variables in order to get the comparisons to happen between the right values, and some jumping out to get AND behaviour in there)

Since it's something that has to be carried out, it isn't the most natural thing to put in code, which is instructions to the machine. It's bit of a trade-off between staying true to what really goes on and being expressive.


JavaScript is an imperative language. It describes what to do. Do this, do that. It doesn't describe what things are, as a person with a math background might be more inclined to do.


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