+ 5

Why is the answer 16 for this question?

what is the output of this code? var sum=0; for(i=4; i<8; i++) { if (i==6) { contuine; } sum +=i; } document.write(sum);

14th Jun 2016, 8:01 PM
cassie
8 Antworten
+ 38
i=4 then i is minor to 8 i is not equal to 6 then sum=4, becasue 0=0+4 -> sum=4 i=5 then i is minor to 8 i is not equal to 6 then sum=9, becasue 4=4+5 -> sum=9 i=6 then i is minor to 8 i is equal to 6 then jum to end the for i=7 then i is minor to 8 i is not equal to 6 then sum=9, becasue 9=9+7 -> sum=16 i=8 then i is not minor to 8, but is equal the the for is end, and the output is 16 for sum
14th Jun 2016, 9:08 PM
Tommaso
+ 3
i thought it was 24, but that was because I was so used to the lessons using <= that i didn't read it carefully enough to see it was actually < only. my goof.
20th Jun 2016, 2:19 AM
DevCoder (John Sullivan)
DevCoder (John Sullivan) - avatar
+ 3
the top comment explains it well but if you really want to simplify it, the answer is basically what is 4 + 5 + 7
6th Sep 2016, 10:41 AM
Jon
+ 2
thank you, I finally understand it. 😊😊😊
14th Jun 2016, 9:12 PM
cassie
+ 2
because this has nothing to do with toast and this forum seems to use html encoding to prevent that arbitrary Javascript most likely due to security. Good job to the forum coders!
20th Jun 2016, 5:43 AM
Neg suki
Neg suki - avatar
+ 2
1st iteration 4, 2nd (4+5)=9, and 3rd( 9+7)=16..
21st Jun 2016, 8:45 AM
Sudhir Dilip Salunkhe
Sudhir Dilip Salunkhe - avatar
0
nice explenation just to add "think in a loop the sum isn't closed if the loop is < then 8 so for exemple remove the if i==6 continue and the awnser is 22 sum=0+4+5+6+7 and the loop stops. because 4+1+1+1+1= not less then 8 its 8 and your command is <8.
18th Jun 2016, 2:52 PM
Josha Grin
Josha Grin - avatar
0
Finally understand this now, thank you very much for the explanation, forgot that the addition wasn't outside the loop
9th Jun 2020, 2:48 PM
David Godwin
David Godwin - avatar