FAQ: Loops - Nested Loops

 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.

1 Like

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

1 Like

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.

1 Like

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?

1 Like

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.

1 Like

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

1 Like

Got it, thanks @stetim94!

if(bobsFollowers[i]===tinasFollowers[j])
{
mutualFollowers.push(tinasFollowers[j])
}
console.log(mutualFollowers);