For Loops - Nested Arrays

https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-iii

So I wrote This Line of Code after the lesson:

let bobsFollowers = ['Leon', 'Clara', 'Leyla', 'Nico'];
let tinasFollowers = ['Carlos', 'Leyla', 'Leon'];
let mutualFollowers = [];

for (let i = 0; i < bobsFollowers.length; i++) {
  for (let j = 0 ; j < tinasFollowers.length; j++) {
    if (bobsFollowers[i] === tinasFollowers[j]){
      mutualFollowers.push(tinasFollowers[j]);
      console.log('Your Mutual Followers are ' + mutualFollowers);
    }
  }
}

And the Output is:

Your Mutual Followers are Leon
Your Mutual Followers are Leon,Leyla

So why does it exactly print Leon first and how can I slove that?

You nested your .log() call within the loop, why? Now each time mutual friends are found, all mutual friends are logged

shouldn’t the flow be to find all mutual friends first, then log all mutual friends?

3 Likes

Hello, and Yes, I just realized it :c
thanks for the Response