5. Nested Loops - How can I loop each element only once in both "for"


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/learn-javascript-loops/lessons/loops/exercises/for-loops-iii

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
I was able to come up with something that solves the issue and I’m quite proud of myself for it(haha), but, I’m pretty sure there’s a better answer. The way I did seems a bit convoluted. Also, I wanted to know if there’s a way to do it using only For loops and not While.

```

let myPlacesIndex = 0;

while (myPlacesIndex < myPlaces.length) {
console.log(myPlaces[myPlacesIndex]);
myPlacesIndex++;

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

<do not remove the three backticks above>

#2

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