Nested loop works weird

Hello. The inner loop should run all its iterations before moving on to continue the outer loop. But the output I get suggests that the inner loop leaves its boundaries too early and does it twice. Any ideas?

The whole code suppose to compare two arrays and fill the third array with the items that exist in both arrays.

UPD: I forgot to put let when I was declaring j and k variables but even that doesn’t help to avoid the unwanted outcome.

When are we expecting to see output from the loop(s)?

1 Like

Yeah, I was expecting to see it after all iterations are done :sweat_smile:. Now I got that the inner loop iterates over its every item comparing it to one item of the different array, then lets the outer loop to log something (if there is anything to log) and to increase the value of j by one. So yeah, the console.log statement should be definitely outside of the outer loop. Thanks.

1 Like