# 5. Nested Loops

#1
``````let myPlaces = ['Place1', 'Place2', 'Place3'];
let friendPlaces = ['Place4', 'Place5', 'Place1'];

for (let myPlacesIndex = 0; myPlacesIndex < myPlaces.length; myPlacesIndex++) {
console.log(myPlaces[myPlacesIndex]);
for (let friendPlacesIndex = 0; friendPlacesIndex < friendPlaces.length; friendPlacesIndex++) {
console.log(friendPlaces[friendPlacesIndex]);
}
}
``````

I don’t understand the logic behind the inner “for loop” looping completely all at once. I imagined the order that should have been printed to the console is: Place1, Place4, Place2, Place5, Place3, Place1.

Can someone explain to me why that is?

#2

lets add some strings so we can see which console.log statement is actually being called:

``````let myPlaces = ['Place1', 'Place2', 'Place3'];
let friendPlaces = ['Place4', 'Place5', 'Place1'];

for (let myPlacesIndex = 0; myPlacesIndex < myPlaces.length; myPlacesIndex++) {
console.log(myPlaces[myPlacesIndex] +" outer");
for (let friendPlacesIndex = 0; friendPlacesIndex < friendPlaces.length; friendPlacesIndex++) {
console.log(friendPlaces[friendPlacesIndex] + " inner");
}
}
``````

as you can see, the nested for loop will loop over all the elements in friendsPlaces for each iteration of myPlaces. So `console.log(friendPlaces[friendPlacesIndex])` will produce 9 outputs total (3 * 3)

what you have in mind is looping over two lists at the same time, which in python would look like:

``````myPlaces = ['Place1', 'Place2', 'Place3']
friendPlaces = ['Place4', 'Place5', 'Place1']

for m, f in zip(myPlaces, friendPlaces):
print m # print(m) for python3
print f # print(f) for python3
``````

of which JS doesn’t have a simple equivalent, which is why i show it in python2.

you can achieve this `zip` in JS as well, but its a lot more complicated:

https://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function

#3

Thanks for the answer. I was interested specifically in the logic behind that, I thought I hadn’t considered some some sort of thought process but I also posted this question on Stackoverflow and I guess the answer to the “why” is “It’s just the way it is.”

#4

behind what? what `that` is referencing is unclear

maybe run your code through a visualizer:

http://www.pythontutor.com/javascript.html#mode=edit

so you can see the inner loop iterating over all elements in friendPlaces for every element in myPlaces.

You nested the loop, nesting loops mean the inner loop will run for each iteration of the outer loop.

#6

By “that” I meant why it behaves like that(iterating all the elements of the inner loop at once for every individual iteration of the outer loop).

I’m sorry if I didn’t make my self clear. My question was merely a theoretical one, about why it works like that by default and not in a “zip” system, iterating one element of the outer loop, then one element of the inner loop, and so on(which to me it makes more sense to be the default behavior).

I just fail to see what part of that JavaScript code implies that the inner loop has to be iterated all at once for each iteration of the outer loop.

The only answer I got remotely close to what I meant with my question was “It’s just they way it is. The JS documentation was made for loops to behave like that”.

(The website you sent me is not working with JS code due to some backend server problems)

#7

Yea, the closest answer you will get there is that is how loops are implemented in Javascript. Which is already so many abstractions layers (processor code, machine code and so on)

but if you wanted to implemented this, then its simply:

``````for (let myPlacesIndex = 0; myPlacesIndex < myPlaces.length; myPlacesIndex++){
console.log(myPlaces[myPlacesIndex]);
console.log(friendPlaces[myPlacesIndex]);
}
``````

the for loop are simply indexes, so we simply lookup the same index (0 for example) in different lists, so that makes no sense to have nested loop implemented the way you suggested (i don’t mean that in a bad way)

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.