1
votes

I try to understand nested for loops in javascript but it's very confusing.

I have this code and I can't understand how it works:

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

In console I have : 0 1 0 1 2 0 1 2 3

And I'm trying to figure out which loop represent each number.

Tip: Run the program yourself using only a pen and paper.Dai
A loop doesn’t “represent” a number. Do you know how a simple loop works, exactly? Read the documentation. A loop executes statements as long as a condition holds. A loop is itself a statement.Sebastian Simon
Include i in the log also and it will probably help you visualize bettercharlietfl