Trouble with loops


#1

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

Why am I getting nicobar instead of lakshadweep?


var friendPlaces = ['chennai','lakshadweep','nicobar'];

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(myPlaces[i] === friendPlaces[j]);
}
}
}

#2

Actually you are getting true instead of lakshadweep.

have a look at this line

console.log(myPlaces[i] === friendPlaces[j]);

what do you think?what’ll it print?

now after printing true it’ll print the next value as for loop is still running.


#3

okay. How do i get it to print what’s common instead of running the for loop?


#4

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