Nested loops not logging out everything I want

Hi! I am going through loop lessons and I have a problem with nested loops I can’t figure out.
I needed to make a code where I compare two person’s follower list and find which of followers appear in both of those lists.

Here is my code:

const bobsFollowers = ['Kate', 'Richard', 'Mike','Dereck'];
const tinasFollowers = ['Dereck', 'Linda', 'Mike'];
let mutualFollowers = ['  '];

for (let i = 0; i < bobsFollowers.length; i++){
     for (let j = 1; j < tinasFollowers.length; j++){
          if (bobsFollowers[i] === tinasFollowers[j]){
               mutualFollowers.push(tinasFollowers[j]);    //I did try to change to bobsFollowers, but it made no difference to a result
          }
      }
};
console.log(mutualFollowers);    //Output: [  '  ' ,  'Mike' ]

Why it is logging only Mike? Dereck also appears in both arrays, but it was left out. I suppose the reason is, that Mike in both arrays has the same index of 2, but Dereck’s index changes. Can I somehow work around this or this is just how things work and I can do nothing?

Also, in instructions to a task, I have everything checked, so instructions took my code as complete, but it still is not working as intented.

Hi!

Thanks for sharing :slight_smile: remember to press </> and format your code so we can understand it more clearly.

Just based on what you’re saying though, I would check 2 things:

  • what is the value of (bobsFollowers[i] === tinasFollowers[j])
  • and before you push, what is the value of tinasFollowers[j]

Additionally whenever I’m doing nested loops I do try to print them out once just to see I’m getting the desired behavior. In this case it might be easy (even unneccesary), but it doesn’t hurt to visualize when troubleshooting.

Thanks for letting me know about </>! I will use it everywhere I can now lol.

  • How do I correctly check that? I tried to log it and in gives me true. Or this is what you asked for?
  • I probably did this one wrong too, I logget tina’s value like this:
for (let i = 0; i < bobsFollowers.length; i++){
    for (let j = 0; j < tinasFollowers.length; j++){ 
         console.log(tinasFollowers[j])  //logged here
        if (bobsFollowers[i] === tinasFollowers[j]){
        ...

and got output like this:

Dereck
Linda
Mike
Dereck
Linda
Mike
Dereck
Linda
Mike
Dereck
Linda
Mike

I probably did everything wrong you asked

No, you did it right.

Now you have more clues to get you closer to what you’re trying to do.

I’m just giving you tips on getting the clues (can’t give answers away in this forum :slight_smile: )