Nested For Loops 6/8 - Code not displaying in console


#1

I’ve completed this lesson, but nothing is actually showing the console. I want to understand if there is anything wrong with my code, or a bug with the lesson.

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


let myPlaces = ['USA','Spain','Paris'];
let friendPlaces = ['China','Japan','Italy'];

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


#2

only if you and your friend visit the same place, its log to the console that there is a match

you and your friend haven’t visited the same place, so no match, so nothing is logged


#3

Right, that makes sense. So ideally I would right an else if statement to be something like ‘There was no match this time’.


#4

we can’t just insert an else if statement, because that would then log each iteration of the loop

then we would need to create a variable (for example match), and set it to false

then if there is a match (within the if clause), set match to true

then after the loop check the value of match, and if there is no match, log that there where no matches found


#5

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