Help with Loop within a Loop

I have difficulty understanding a javascript code here.

Can I get some help?


let size = 8;

let board = “”;

for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}

console.log(board);


I don’t really understand how a loop within a loop works.

For the code above, when y = 0, the loop below will be executed until x = 7 right?
then the board += “\n” takes place to start a new line.

Then now y = 1 and we loop it again right?
But does x now still start at = 7?

Not too sure how it works.

1 Like

Hey @Cryptonewb, hope you are ok.

I think this video can explain it quite well how nested loops works. :nerd_face:

https://www.youtube.com/watch?v=piKpw_CghS8

CarlosZ