I don't understand why the answer is 5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't understand why the answer is 5

While b-- goes until b=1 or b = 0 https://code.sololearn.com/WnIycVP5ayBI/?ref=app

27th Aug 2021, 6:48 PM
Heidi Eigner
4 Answers
+ 3
while(3) //b=2 a=3 while(2) //b=1 a=4 while(1) //b=0 a=5
27th Aug 2021, 6:55 PM
Abhay
Abhay - avatar
+ 2
Heidi Eigner while loop will work until b becomes 0 and at the same time a will be increase by 1 so here b will be decrement by 1 and a will be increment by 1 so when b becomes 0, a becomes 5.
27th Aug 2021, 8:04 PM
A͢J
A͢J - avatar
+ 1
You may find it helpful to output a and b on each iteration to see what's going on while(b--) {++a; console.log(a + ", " + b);};
27th Aug 2021, 7:19 PM
Lisa
Lisa - avatar
0
This is perfect. Thanks
27th Aug 2021, 7:21 PM
Heidi Eigner