5. For Loops Inside For Loops


#1

Why is that ? It's not true.
Shouldn't it be irrelevant if the last line looks like:

console.log('Match: ' + friendPlaces[i]);

either

console.log('Match: ' + myPlaces[i]);

In first case result is as on the picture and in the second one, result is correct, which is "Japan" - it's indeed common place for myPlaces and friendPlaces.

I include the below for eventual checking purposes:

var myPlaces = ['USA', 'Japan', 'Otwock'];
var friendPlaces = ['Japan', 'Poland', 'Australia'];

for (var i = 0; i < myPlaces.length; i++) {

for (var j = 0; j < friendPlaces.length; j++) {

if (myPlaces[i] === friendPlaces[j]) {
  console.log('Match: ' + friendPlaces[i]);
}

}

}


#2

should be, friendsPlaces[j]


#3

Ahhhhhhh I see, its clear now :slight_smile: Thanks

but I'm still curious why the result is what it is :wink: It doesn't matter much though...


#4

You were printing friendsPlaces[1], which is 'Poland', when you should have printed friendsPlace[0], 'Japan'. myPlaces[1] is 'Japan', that's why you cannot use i to point to the element in friendsPlaces.


#5

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