For loop keeps repeating. how do i stop it?

Hello. I am currently learning JS and am currently learning about for loops. Why does code repeat 3x? Also any way to improve(better) the way I wrote this would be appreciated by a rookie coder. Thanks!

Hello @danielmills8808, welcome to the forums! This is because you have two loops-one nested in each other. The first loop loops through the favOrder array-which is three times. Then the second loop loops through the favRappers array-which, since it length is three, the loop loops three times.

So, foreach iteration of the first loop, there are three iterations of the second loop. Therefore, 3*3 is 9; hence why there’s 9 lines printed to the console.

1 Like