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.

3
  • 1
    You don't need continue;. Loops automatically continue unless you do something to stop it. Commented Aug 17, 2021 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. Commented Aug 17, 2021 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. Commented Aug 17, 2021 at 19:13

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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

1 Comment

These calculations really helped in the end when I laid out the code. Thank you so much @Barmar
0

This loop is calculated like this.

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

So sum = 20 + 5 = 25

2 Comments

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! :)
I finally understand how this fits into the code! Thank you very much for the explanation! It is very much appreciated @Kirill-Savik
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);

2 Comments

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!
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.
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) 

4 Comments

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?
Exactly @AstonJay
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
Thank you so much! I figured it out and I understand it now :D @Hozeis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.