The result of the code is 9 but I don't understand why. Please someone should help explain to me why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

The result of the code is 9 but I don't understand why. Please someone should help explain to me why?

var count = 6, sum = 0; while(count>0) { sum+=3; count-=1; if(count==3) { count = 0; } } document.write(sum);

7th Jul 2019, 10:46 AM
eMBee
eMBee - avatar
7 Answers
+ 5
Thanks ~ swim ~, that explanation was clear and precise
7th Jul 2019, 11:32 AM
eMBee
eMBee - avatar
+ 3
Count = 6, 5, 4 Sum = 3, 6, 9 😊
8th Jul 2019, 4:03 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Ez, to those who still don't understand, it's looping count -= 1 simply by subtracting 1 and setting it as the new variable value, until it reaches 3 from 6... Which is three times. In those three times the code is also adding 3 to sum that was originally 0... And 0 plus 3 three times makes 9. Hope that helps.
7th Jul 2019, 7:19 PM
Momo
Momo - avatar
+ 1
👍
13th Jul 2019, 8:15 PM
Karen Navarro Cedeño
Karen Navarro Cedeño - avatar
+ 1
👍
13th Jul 2019, 8:15 PM
Karen Navarro Cedeño
Karen Navarro Cedeño - avatar
- 1
🙂
7th Jul 2019, 4:34 PM
Srilakshmi Syagamreddy
Srilakshmi Syagamreddy - avatar
- 1
Tq you are clarify my doubt
7th Jul 2019, 4:37 PM
Srilakshmi Syagamreddy
Srilakshmi Syagamreddy - avatar