Why do the loops work? (How do they ever match?)

Apologize if this has been answered already (I didn’t see the question asked yet)

why does this work?

const bobsFollowers = ['Jim', 'Joe', 'Jerry', 'John'];
const tinasFollowers =['Jessica','Jim', 'Joe'];
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(bobsFollowers[i]);
    }
  }
}
console.log(mutualFollowers)

It seems to me that the array would never match the names as the indexes are off by 1 (Jim is index 0 in bob’s array and index 1 in tina’s) When index 0 for bob’s array is called, it gets Jim, when index 0 is called for tina’s, it gets Jessica… How would these ever equal each other? Very likely I misunderstanding some concept…

thanks in advance.

21 Likes

the loops are nested, so for each friend of bob, we loop over all the friends of tina.

first friend of bob, loop over all friends of tina, and compare each friend of tina with first friend of bob
second friend of bob, loop over all friends of tina, and compare each friend of tina with second friend of bob
etc etc.

we can see this if you like:

for (let i = 0; i < bobsFollowers.length; i++) {
  for (let j = 0; j < tinasFollowers.length; j++) {
    console.log(i, j, bobsFollowers[i], tinasFollowers[j]);
    if (bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers.push(bobsFollowers[i]);
    }
  }
}
55 Likes

Ah i see it now… Thanks!

Thanks for this, really nice simple and concise way of explaining it, all too often when you check something on stackoverflow as a newbie, you leave feeling more confused than I did before I started.

8 Likes

This is great, however I’m having a bit of trouble conceptually here:

console.log(i, j, bobsFollowers[i], tinasFollowers[j]);

How come bobsFollowers[i] becomes Bob’s follower’s name? Is it because as “i” iterates upwards, it becomes like this:
bobsFollowers[0]
bobsFollowers[1]
bobsFollower[2]

So it’s actually calling the individual elements within the array while it’s in the inner for loop?

bobsFollowers[i] looks up elements by index in the bobsFollowers array.

i is increased by the loop

2 Likes

Got it, thanks @stetim94!

Hey when you are comparing ‘bobsFollowers[i] === tinasFollowers[j]’
Can you explain what is happening?

e.g. is it going array 0 === array 0? array1===array0? array2===array0?
array3===array0? array1===array1? and so on ???

thanks

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

because j is the inner loop it will loop through all of tina’s followers everytime i is incremented.

so:
i = 0 -> loop trough tina’s followers and check if bobsFollowers[0] is equal to tinasFollowers[j]
i = 1 -> loop trough tina’s followers and check if bobsFollowers[1] is equal to tinasFollowers[j]
i = 2 -> loop trough tina’s followers and check if bobsFollowers[2] is equal to tinasFollowers[j]
i = 3 -> loop trough tina’s followers and check if bobsFollowers[3] is equal to tinasFollowers[j]
etc.

6 Likes

I am trying really hard to understand this section of the correct code;
if (bobsFollowers[i] === tinasFollowers[j]) {
** mutualFollowers.push(bobsFollowers[i]);**

It seems that we asking that if the index of both of these variables are EQUAL (which they aren’t) then we want to push the complete index of bobsFollowers into the mutualFollowers variable. This somehow results in mutualFollowers receiving the matching values of both arrays. Am I misunderstanding this code???

Complete code is below;

// Write your code below
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]);
}
}
};

@methodslayer80609
No this is not the case.
bobsFollowers[i] gives the value and not the index.
The if statement will therefor check if the value of bobsFollowers[i] is the same as the value of tinasFollowers[j]

i = the index
j = the index
bobsFollowers[i] = the value
tinasFollowers[j] = the value

2 Likes

okay, i think a follow a little here. So that code is really saying " WHERE we have equal values then please push those equal values to mutualFollowers ; " It is not asking if the values of both arrays are identical in their entirety

and thank you by the way

It is not checking if both arrays are identical no.
The values that you get by calling bobsFollowers[i] and tinasFollowers[j] do need to be identical.

try logging the code with console.log(bobsFollowers[i]) and see what you get.

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++) {
        console.log("current index: "+i+" current Bob follower: " + bobsFollowers[i]); // print current bob follower to log
        console.log("current index: "+j+" current Tina follower: " + tinasFollowers[j]);// print current tina follower to log
        if (bobsFollowers[i] === tinasFollowers[j]) {
            mutualFollowers.push(bobsFollowers[i]);
            console.log("Bob and Tina share the follower " + bobsFollowers[i] + " and is added to mutualFollowers .");
        }
    }
};
console.log("Bob and Tina share the following followers: ");
console.log(mutualFollowers);  // print all mutualFollowers to console.

After you run this you can see in the console what is happening at what moment. It might help you understand what everything does.

Also try changing the names of bob and tina’s followers and see how this changes the log. For example: now Marta should be shared between bob and tina so it should be added in mutualFollowers but if you add a space for Martha in bobs followers they suddenly don’t share Marta anymore.

6 Likes

OMG i think i get it. bobsFollowers[i] === tinasFollowers[j] is comparing if the CURRENT followers are a match. And since they are all compared to each other at least once because of our nested loop we do eventually have 2 instances of matching values and therefore they get added to the mutual followers.

Does this sound correct-ish?

2 Likes

Yes, that is the idea. Iterate over one list and compare each item in that list to each item in the other list, by iterating over that one.

Later on we learn about iterators in JS which help to demystify this operation.

matches = list1.filter(x => list2.includes(x))

Eg.

 > list1 = [2,4,6,8,10,12]
<- (6) [2, 4, 6, 8, 10, 12]
 > list2 = [3,6,9,12,15,18]
<- (6) [3, 6, 9, 12, 15, 18]
 > list1.filter(x => list2.includes(x))
<- (2) [6, 12]

Ignore this example until you get to the unit on iterators. I only introduce here as a comparison and illustration of what the nested loop exercise is expected to produce: A list of elements both lists have in common.

4 Likes

v wanted to push the common friends to a new list mutualFriends, why is bobsFollowers[i] mentioned here?

Given the values are equal (thanks to the if condition), you could push either one. You could also use tinasFollowers[j].

4 Likes

No I mean mutualFollowers.push should ve done the trick

Can you show the full code how you intended this?

you still need to specify what you push to the array, the code doesn’t magically know this.

Ok my understanding did improve with ur reply that the code doesn’t know magically what to push. But here’s the code
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]);
Why does it push bobsFollowers[i], should v not have a way to push only the common followers? I’m just a beginner.