+ 3
var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); the output is 16.... can a
22 Réponses
+ 42
i hate life
+ 13
4+5+(skip 6 because of "continue")+7=16
+ 11
Hi! I will difinitely check it step by step on http://www.pythontutor.com/live.html#mode=edit
I just added this code to make it run:
let sum=0;
let i=4;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum);
It will show you step by step how it works and why it gives 16!
I was also crazy about this answer, so here is the answer:
When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" stays/sits on the sum waiting for the next itineration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9!
NOw "sum" valu is 9.
6 is ignored, and here comes the 7!
So now 7 + 9 = 16!
I know is kind of messy from my side, but hopes it helps someone.
+ 3
I just added this code to make it run:
let sum=0;
let i=4;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum);
It will show you step by step how it works and why it gives 16!
I was also crazy about this answer, so here is the answer:
When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" stays/sits on the sum waiting for the next itineration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9!
NOw "sum" valu is 9.
6 is ignored, and here comes the 7!
So now 7 + 9 = 16!
I know is kind of messy from my side, but hopes it helps someone.
+ 2
The tricky part is that the "document.write()" is outside of he loop.
So after "for..." is looped all around, only then comes the printed result with the last possible "i" in the "for" loop i.e 7.
If you simply put the "document.write()" before the last "}" of the loop it will print what all of us - the beginners expected before thinking it through.
P.S don't forget the + "<br />" so the print is easier to look at:
var sum=0;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
document.write(sum + "<br />");
}
+ 2
answer theory
1. (sum declared as 0) sum = (0 + next iteration of loop) --outside of loop
2. (sum declared as 4) sum = (4 + next iteration of loop)
3. (sum declared as 5) sum = (4+5+next iteration of loop)
4. (skip 6)
5. (sum declared as 7) sum = (4+5+7)
6. stop loop because 8 is equal to 8 not less than
7. output = (0 + 4 + 5 +7) = 16
+ 1
you kristina keep going
+ 1
16
+ 1
so if 4 =i++ equals 6 it ignores it moves on to the next number.
but if i=4;
i<8;
if i==6;
wouldnt you take 6+8+2 to get the answer.
+ 1
1054 Comments
What’s the output of this code?
var sum=0;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum);
16
Ans:-
First checking for loop, (i=4; 4<8; 5), (i=4; 4<8; 6), (i=4; 4<8; 7). Now, got our for loop result. next step is if condition (5==6) (6==6) (7==6). Now here is 7 and 5 is not == 6, so we have now 5 and 7 Okay!. next step is if == 6 thn continue otherwise sum+=i means add it by sum value okay!. and sum values are = 4,5,7. so adding now= (0+4+5+7) = 16 . I hope it will help you.
+ 1
16
0
thx
0
Kristina, please can't you put a comment like this here. thx
0
I Things its total of 4+5+7 = 16
0
16
0
jovob 16
0
I got 16 and still can't explain it. This is so hard, but I'll get it though.
0
It seems so tricky though!! I just added this code to make it run:
let sum=0;
let i=4;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum);
Let me show you how it becomes 16!
It so funny about this answer, so here is the answer:
When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" remains on the sum waiting for the next iteration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9!
Now "sum" value is 9.
6 is ignored, and here comes the 7!
that's why, now 7 + 9 = 16!
0
0
16