JavaScript - loop 5/7


#1

why does the var 'friendPlaces' print out 3 times ?


#2

@_access, we can't help you without code. Please, post your full code.


#3

for (var i = 0; i < 3; i++) {
    console.log(myPlaces[i]);  // will print 3 times in for i loop

    for (var j = 0; j < 3; j++) {
        console.log(friendPlaces[j]);  // in this for j loop, will print 3 times, but for i loop + for j loop will make friendPlace[j] print 3 * 3 times. in others way, print every myPlace element 1 time, will print 3 times of friendPlace element.
    }
}

#4

The inner loop prints out all of your 'friendPlaces' everytime it runs. Because it is nested within the outer'myPlaces' loop, it executes once for every iteration through the outer loop.

Incidentally, make sure to follow the lesson's instructions and compare 'i' to 'myPlaces.length'. That way if your array grows your loop will still cover it. Using a hard coded '3' is less flexible.