Why is the second element is 'undefined'?

Hello,

I was trying to print the names of the mutual followers to the console, however, the second mutual follower’s name is ‘undefined.’ Why is this happening? :crazy_face:

const bobsFollowers = ['Chris', 'Jake','Roland', 'Susan'];
const tinasFollowers = ['Susan', 'Chris', 'Tatiana'];
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[i]);
    }
  }
}    mutualFollowers.push(bobsFollowers[i]);

Hi,
I would rather expect your code to throw a reference error than logging anything like just ‘undefined’ in the first place.

This is outside of your loop, so i is undefined. Are we seeing all of your code?

I’m sorry that I added the last line in the code.
My code is

const bobsFollowers = ['Chris', 'Jake','Roland', 'Susan'];
const tinasFollowers = ['Susan', 'Chris', 'Tatiana'];
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[i]);
    }
  }
}

I still see Chris and undefined in the console. I expected to see Chris and Susan…

The outer loop runs four times, because bobsFollowers has 4 items. So the last time the outer loop runs, i === 3. The first time your condition isn’t true, the mutualFollowers array has one item less than the current i. So there is no mutualFollowers[i].

When your in the inner loop for the first time and i===0, mutualFollowers.length ===1. Because the item is pushed to the array. Then mutualFollowers[i] logs something valid.
If the second time the outer loop runs the condition is false, mutualFollowers.length is still 1.

2 Likes

When you do:

console.log(mutualFollowers[i])

You are using i (a reference used to loop through bobsFollowers) to try to access an index in mutualFollowers. But the location in mutualFollowers is not tracked by either i or j so you can’t use any of those on mutualFollowers. They refer to locations in bobsFollowers/tinasFollowers respectively. Your code works and is building up the correct array which you can see if you do this after the for-loops:

console.log(mutualFollowers);
2 Likes

Thank you! Then is there any way to track an index by using arbitrary alphabets like i for bobsFollowers or j for tinasFollowers?

You could push the item as an object with a key for the index into the array like:

`mutualFollowers.push({
  name: bobsFollowers[i],
  idx: i
});`
1 Like

Thank you! Your explanation made me wonder how many times the loops run and how could the loops come up with Chris and Susan as mutualFollowers.

First run: outer loop at i = 0 (Chris) does not equal inner loop at j = 0 (Susan) → false
Second run: outer loop at i = 1 (Jake) does not equal inner loop at j = 1 (Chris) → false
Third run: outer loop at i = 2 (Roland) does not equal inner loop at j = 2 (Tatiana) → false
Fourth run: outer loop at i = 3 (Susan) no inner loop at j = 3 → false

If the loops run in this sequence, there would be no mutualFollowers. Then, in what sequence do loops should run?

For each outer loop, the inner loop runs three times. So 12 in total.

1 Like

I see, thanks a lot!! :grinning:

1 Like