1

I got a quiz question in my Full Stack Web Development online course that states the following:

let sum = 0;
for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 2; j++) {
        sum = sum + i + j;
        continue;
    }
}
console.log(sum);

Now, the correct answer to the question is 25 according to the answer of the quiz, but I have no idea how it gets to 25? The closest I get when writing it out on paper to try and visualize it, is either 15 / 16.

Could someone please write a visualization that would make it clearer for me on how this nested loop gets to 25?

Thanks in advance.

AstonJay
  • 13
  • 5
  • 1
    You don't need `continue;`. Loops automatically continue unless you do something to stop it. – Barmar Aug 17 '21 at 19:08
  • Yes, this was explained in the answer to the quiz that the "continue;" is basically useless and was placed there as a "test" due to it being a quiz. But I am trying to visualize how it gets to the number 25? I know I must be missing something simple, but do not know what I am not seeing/realizing. – AstonJay Aug 17 '21 at 19:13
  • 1
    How do you get to 15 or 16? Without knowing this, we don’t know where you went wrong. Please [edit] your question to provide details. – Sebastian Simon Aug 17 '21 at 19:13

5 Answers5

2

Add a console after second for, you should see the visualization

let sum = 0;
for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 2; j++) {
        console.log(`sum=${sum} i=${i} j=${j}`)
        sum = sum + i + j;
        continue;
    }
}
console.log(sum);

//output
// sum=0 i=0 j=0
// sum=0 i=0 j=1 
// sum=1 i=1 j=0 
// sum=2 i=1 j=1 
// sum=4 i=2 j=0 
// sum=6 i=2 j=1 
// sum=9 i=3 j=0 
// sum=12 i=3 j=1
// sum=16 i=4 j=0
// sum=20 i=4 j=1
// 25
R Nair
  • 116
  • 3
  • Hi R Nair, Thank you for the reply. Your visualization is good, I will have to look through it to gain a better understanding of why ' i ' seems to have the same value twice, and why ' j ', "resets" back to zero and then 1 again. As I've stated in my reply to Barmar above, I did not expect the replies so quickly :D. Thank you very much. – AstonJay Aug 17 '21 at 19:30
  • I got it! Thanks you so much! I had to look at it for a while, but I finally know how it works. :D @R-Nair – AstonJay Aug 18 '21 at 20:18
2

Here are all the iterations of the loops, and the value of sum after each.

i = 0 j = 0 sum = 0 + 0 + 0 = 0
i = 0 j = 1 sum = 0 + 0 + 1 = 1
i = 1 j = 0 sum = 1 + 1 + 0 = 2
i = 1 j = 1 sum = 2 + 1 + 1 = 4
i = 2 j = 0 sum = 4 + 2 + 0 = 6
i = 2 j = 1 sum = 6 + 2 + 1 = 9
i = 3 j = 0 sum = 9 + 3 + 0 = 12
i = 3 j = 1 sum = 12 + 3 + 1 = 16
i = 4 j = 0 sum = 16 + 4 + 0 = 20
i = 4 j = 1 sum = 20 + 4 + 1 = 25
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

This loop is calculated like this.

(1+2+3+4) * 2 = 20 (0+1) * 5 = 5

So sum = 20 + 5 = 25

Kirill Savik
  • 1,228
  • 4
  • 7
  • Hi Kirill Savik, Thank you for your reply! It is much appreciated. I think I am starting to gather some thoughts while reading through all the replies about how it all fits together. I'm not all the way there yet, but I think I am starting to figure it out. Thank you again! :) – AstonJay Aug 17 '21 at 19:42
  • I finally understand how this fits into the code! Thank you very much for the explanation! It is very much appreciated @Kirill-Savik – AstonJay Aug 18 '21 at 20:24
0

Perhaps this will give you more visualization of on what's going on:

let sum = 0;
for (let i = 0; i < 5; i++) {
console.log("loop start");
console.log("i", i);
    for (let j = 0; j < 2; j++) {
console.log("sum", sum);
console.log("j", j);
        sum = sum + i + j;
        continue;
    }
console.log("loop end");
}
console.log(sum);
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • Hi Vanowm, Thank you very much for the reply! The code snippet runs perfectly! I have to study it more closely to wrap my head around it along with the previous replies. Again, thank you all so much! This community is amazing! I will look though this and once I get that light-bulb moment, I will post or vote or whatever needs to be done. I'm new to stack-overflow, so I will figure this out too. Thank you very much again! – AstonJay Aug 17 '21 at 19:35
  • Thank you @vanowm for the code snippet. You guys really helped me to get it to "click" after I studied your examples and playing around with the code. It is much appreciated. – AstonJay Aug 18 '21 at 20:22
0

The left side would be the values of i and j:

0 // i
0 1 // j  // sum = 0 | sum = 1 + (0) <-- () is previous value of sum
        
1
0 1       // sum = 1 + (1) | sum = 2 + (2)
     
2
0 1       // sum = 2 + (4) | sum = 3 + (6)

3
0 1       // sum = 3 + (9) | sum = 4 + (12)

4
0 1       // sum = 4 + (16)| sum = 5 + (20) 
Hozeis
  • 1,542
  • 15
  • 36
  • 1
    Hi Hozeis, If I understand correctly, the for-loop for ( j ) runs twice every time, but it resets its own value from 1 back to zero, at the start of each iteration of the for-loop for ( i )? If that makes sense? – AstonJay Aug 17 '21 at 19:47
  • Exactly @AstonJay – Hozeis Aug 17 '21 at 19:51
  • 1
    Wow, ok cool! So I got that, I am going to spend a little more time with the answers here, but I think that I almost got it! :D Thank you! @Hozeis – AstonJay Aug 17 '21 at 19:55
  • Thank you so much! I figured it out and I understand it now :D @Hozeis – AstonJay Aug 18 '21 at 20:20