5. For Loops Inside For Loops


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/loops/exercises/for-loops-iii


"Did you write an if/else statement inside the second for loop?"


var myPlaces = ["a", "b", "c"];
var friendPlaces = ["d", "a", "e"];
for(var i = 0; i < myPlaces.length; i++) {
  console.log(myPlaces[i]);
  for(var j = 0; j < friendPlaces.length; j++) {
    if (myPlaces[i] == friendPlaces[i]) {
      console.log(myPlaces[i]);
    };
    };
  };


#2

Oh my god. facepalm

Just realised my mistake and fixed it. Gosh, JS can be so intricate haha.

In the condition of the if statement, I changed '==' to '===' AND I changed 'friendPlaces[i]' to 'friendPlaces[j]'.


#3

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