0

answer is 3 but can someone please explain how and why :

let a,b,c; a=1; b=5; c=7; if(++b+a > b++){ console.log(b-4) }

9th Feb 2020, 5:19 AM
BangArray
BangArray - avatar
7 Answers
+ 2
in the condition, there are left hand side and right hand left hand side: ++b+a right hand side: b++ left hand side is evaluated first ++b+a is (++b) + a a is 1 b is 5 ++b is 6, so (6) + 1 so left hand side is 7 right hand side is b++ b is 6, so right hand side is 6. then, b++ so b becomes 6 + 1 becomes 7.
9th Feb 2020, 6:38 AM
Gordon
Gordon - avatar
+ 3
in the condition, there is ++b and b++ https://www.sololearn.com/learn/JavaScript/1130/ They both increases b by 1
9th Feb 2020, 5:29 AM
Gordon
Gordon - avatar
+ 2
++b is pre-increment, b becomes 6, and left hand side is 6+1=7 b++ is post-increment, right hand side is 6, and then b becomes 7. Because 7 > 6 is true, log 7 - 4 which is 3 p. s. this is bad practice. only good for brain teaser. don't write in this way in real projects.
9th Feb 2020, 5:25 AM
Gordon
Gordon - avatar
+ 1
But the value of b is 5 , How come log 7-4 But not 5-4???
9th Feb 2020, 5:29 AM
BangArray
BangArray - avatar
+ 1
So value of b becomes 7.. Thank you 🙏
9th Feb 2020, 5:32 AM
BangArray
BangArray - avatar
0
You need to also know why it is comparing 7 > 6
9th Feb 2020, 5:33 AM
Gordon
Gordon - avatar
0
I got that but couldn't understand how b value became 7
9th Feb 2020, 5:36 AM
BangArray
BangArray - avatar