Help Understanding Syntax on Nested Loops (5/8)


#1

I’m not so much “stuck” as I don’t understand why things are a certain way. The code I’ve posted below is correct for the exercise, but can someone elaborate on why/how (myPlaces[myPlacesIndex] === friendPlaces[friendPlacesIndex]) ? The === is a strict equality indicator, right? Yet the strings associated with these two variables are not strictly equal. How does it pull out the ones that are?

Also, when logging the result to the console, why do I call only the first variable? Why isn’t friendPlaces mentioned at all? How is it that this returns the match?

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

I simply don’t understand what’s going on in the background here, so any help you could provide would be much appreciated. I hope to pay it forward someday!

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

const myPlaces = ['Italy', 'Bolivia', 'Chile'];
const friendPlaces = ['China', 'Chile', 'Spain'];

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

'Chile'==='Chile'

why isn’t it strictly equal?

Both have same values and types.

Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.

and here

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

why do you need to mention friendPlaces ?

myPlaces[myPlacesIndex] and friendPlaces[friendPlacesIndex] both have same values.right?
we just want to print the matching place to the console then either you can print myPlaces[myPlacesIndex] or friendPlaces[friendPlacesIndex].It doesn’t matter.


#3

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