Giving me an error

// Write your code below
const bobsFollowers = [‘Bryan’,‘Steph’,‘Mary’,‘Patrick’]
const tinasFollowers = [‘Yen’,‘Steph’,‘Mary’];
const mutualFollowers = ;

for (let i = 0; i < bobsFollowers.lenght; i++){
for(let n = 0; n < tinasFollowers.lenght; n++){
if (bobsFollowers[i] === tinasFollowers[n]){ mutualFollowers.push(bobsFollowers[i])
}
}
};

I don’t why is still giving me an error, can you help me?

its .length not .lenght :wink:

1 Like

let bobsFollowers = [‘Joe’, ‘Marta’, ‘Sam’, ‘Erin’];
let tinasFollowers = [‘Sam’, ‘Marta’, ‘Elle’];
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]);
}
}
};

Hi!
I don’t understand this line of code " mutualFollowers.push(bobsFollowers[i]); " I know we must .push() the mutual followers from bob and tina, but why after declare .push(), we call again (bobsFollowers[i] ???

Nested Loops

It’s arbitrary which one we push in since they are both the same value. What 's important is that we use the correct index. bobsFollowers[i] or tinasFollowers[j].

I also thought that until I tested it but then I got this:

const bobsFollowers = [“Gary”, “Gordon”, “George”, “Gregory”];
const tinasFollowers = [“Sally”, “Gordon”, “Gregory”];
const 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[i]);
}
}
}
console.log(mutualFollowers);
//Logs [ ‘Gordon’, undefined ]

What would the reason be for the first one running and the second undefined?

When I fix it with:
mutualFollowers.push(tinasFollowers[j]);

I get the outcome expected, but why were they not BOTH “undefined” when I used [i]

mtf didn’t say that the indexes i and j were interchangeable. Rather,

mutualFollowers.push(bobsFollowers[i]);
// does the same thing as
mutualFollowers.push(tinasFollowers[j]);

// Either of the two statements above will push 
// the correct element to mutualFollowers

Swapping the indexes doesn’t work. The if condition is not checking whether the indexes of the two arrays are the same. The condition is checking whether the elements at the respective indexes of the two arrays are the same.

// Not correct because j is being used to iterate over tinasFollowers array
mutualFollowers.push(bobsFollowers[j]);
// Not correct because i is being used to iterate over bobsFollowers array
mutualFollowers.push(tinasFollowers[i]);

Sheer luck for first one even though the code is incorrect.

// You wrote:
const bobsFollowers = ["Gary", "Gordon", "George", "Gregory"];
const tinasFollowers = ["Sally", "Gordon", "Gregory"];

"Gordon" is at index 1 of both arrays, so swapping i and j doesn’t cause a problem by coincidence. But "Gregory" is at index 3 of bobsFollowers and at index 2 of tinasFollowers, so swapping i and j does make a noticeable difference. There is no index 3 in tinasFollowers, so pushing tinasFollowers[3] would push undefined to mutualFollowers.

If the arrays were:

const bobsFollowers = ["Gordon", "Gary", "George", "Gregory"];
const tinasFollowers = ["Sally", "Gordon", "Gregory"];

then swapping i and j as per your posted code would cause mutualFollowers to be
[ 'Sally', undefined ] which is clearly incorrect.

Swapping i and j breaks the logic of your code and makes it incorrect.

2 Likes