+ 2

<script>var x=6; var y=0; while (x>0 && y<6) { x--; y= y+ x;} document.write(y)<\script>

Anyone can me explain what is the logic behind this code?

4th Apr 2019, 7:51 AM
KHUSHbu
KHUSHbu - avatar
1 Resposta
+ 7
The output will be 9. In first loop: x=6 x>0 // true, x is 6 y<6 // true, y is 0 so x-- makes x = 5 and y = 5+0=5 // y=y+x so y = 5 In next loop: x>0 // true, x is 5 y<6 // true, y is 5 so x-- makes x = 4 and y = 5+4=9 // y=y+4 so y = 9 and document.write(y) outputs 9.
4th Apr 2019, 8:15 AM
VEDANG
VEDANG - avatar