Need help with Nested Loops in Learn JavaScript: Loops (Exercise 6 - Nested Loops)


#1

Hi,

I’m working on the Nested Loop Assignment and it says I have completed it. But my code only produces ‘1 2’. However, it should say ‘Jack Phil’. Any help would be greatly appreciated.

// Write your code below
let bobsFollowers = ['Joe', 'Jill', 'Jack', 'Phil'];
let tinasFollowers = ['Mike', 'Jack', 'Phil'];
let mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++) {
  for (let j = 0; j < tinasFollowers.length; j++) {
    if (bobsFollowers[i] === tinasFollowers[j]) {
      console.log(mutualFollowers.push(bobsFollowers[i]))
    }
  }
};

#2

I solved my issue. I was trying to do to much with the console.log in my if statement.

// Write your code below
let bobsFollowers = ['Joe', 'Jill', 'Jack', 'Phil'];
let tinasFollowers = ['Mike', 'Jack', 'Phil'];
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(bobsFollowers[i]);
    }
  }
};
console.log(mutualFollowers);