+ 2
JS Challenge Question - Help Needed Please
I am having trouble making sense of this JavaScript challenge questionâs answer: var a = 10, b = 20, c = 30; If (c > b > a) { console.log(â1â); } else { console.log(â2â); } Why is the answer not 1? I searched the discussions and couldnât find anything referencing this question. Thanks for any help anyone can give me!
2 RĂ©ponses
+ 4
When you do
c > b > a
The expression is evaluated as
(c > b) > a
and is reduced to
true > a
The boolean value 'true' is casted to 1 thanks to JS's dynamic type system.
1 > a
which is then evaluated as false.
2 is printed.
Instead, try doing
if (c > b && b > a)
+ 2
Hatsy Rei thanks for the answer! that definitely broke it down to simpler terms and I get it now!