5. For loop inside of for loop


#1


https://www.codecademy.com/courses/learn-javascript/lessons/loops/exercises/for-loops-iii?action=lesson_resume


The programm tells me that the output is correct..... but this is my output "
USA
UK
USA
Match USA
Spain
Canada
UK
USA
Spain
Candyland
UK
USA
Spain

why does it post all the inputs again ??
Here is the code....

var myPlaces = ["USA","Canada","Candyland"];
var friendPlaces = ["UK","USA","Spain"];

for (var i = 0; i < myPlaces.length; i++) {
    console.log(myPlaces[i]);
  
  for (var j = 0; j < friendPlaces.length; j++) {
        console.log(friendPlaces[j]);
    
    if (myPlaces[i] === friendPlaces[j]) {
    console.log("Match " + myPlaces[i]);
    }
  }
}

JavaScript - For loops inside of for loops - Output Query?
#2

1.First loop, loops through myPlaces and prints all items of that list.
2.Second loop, loops through friendPlaces and prints all items of the list.
3.inside if ,the common countries are printed
so overall it seems like all things get printed but not common ones!

to see common countries you can delete console.log() from both loops.


#3

Thanks !!
it's so simple.... but just looks so stupid xD


#4

:slight_smile:
Youre welcome and keep up the good work! :thumbsup:


#5

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