+ 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!

20th Aug 2019, 1:35 AM
Jason Thomas
Jason Thomas - avatar
2 Answers
+ 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)
20th Aug 2019, 1:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Hatsy Rei thanks for the answer! that definitely broke it down to simpler terms and I get it now!
20th Aug 2019, 1:56 AM
Jason Thomas
Jason Thomas - avatar