+ 13

JAVASCRIPT CHALLENGE QUESTION

Someone help. Why is the output 9: var x = 6, y = 0; while (x>0 & & y<6) { x--; y+=x; } document.write(y)

22nd Jul 2020, 11:03 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
6 Antworten
+ 15
in first iteration x :5 y: 5 condition x >0 & & y <6 // true second iteration x: 4 y: 9 condition x >0 & & y<6 // false because 9 >6
22nd Jul 2020, 11:12 AM
Shahghasi Adil
Shahghasi Adil - avatar
+ 9
your while loop executes twice. first 6 is decremented by 1, then added to y (which is 0). y now equals 5. the second time the loop goes through, it adds 4 to y and stops because y is now greater than 6. y is now 9, your loop stops and writes 9
22nd Jul 2020, 11:09 AM
Slick
Slick - avatar
+ 8
Thanks men. I did not read it properly 🤦🏽‍♂️
22nd Jul 2020, 11:11 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 4
First condition X-- (6-1=5) Y+x (5+0=5) Second condition X--(5-1=4) Y+x(5+4=9) So output will be 9
23rd Jul 2020, 2:01 AM
Akram Ben Sidhom
Akram Ben Sidhom - avatar
+ 3
Thanks Guys! Well understood... 👍🏼 🌼
22nd Jul 2020, 11:31 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar