Getting stuck with Loops

Hello,

I’m feeling I’m at the stage of giving up. I’m reading and reading and I’m not understanding loops… For example i = 0, but the exercise I am working on is asking for names…

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]);
}
}
};

Part of the Nested Loops section. But why are we equaling 0 from i… when I’m trying to print a name to console in theory. I don’t understand this Erghhh Also can someone please explain why we are using the .length parameter when we don’t actually need to work out the length of it , we just need to find out if Sam and Marta are in the same group.

Hello @thomasgm-online, welcome to the forums!
So, you really have two questions:

and


So, to answer the first question:
When you set let i = 0, you create a variable which stores the value 0. This increments every iteration of the loop, due to the i ++ line. I’ll get how it knows when to stop later. The reason you do this is that every element in an array has an index. The index of an array start at 0:

let arrayOne = ["name", "othername", "different name"];
                 ^        ^           ^
                 |        |           |
index:           0        1           2

What you use the i variable for is to reference the index of the elements in an array. If I wanted to get the first element of the array arrayOne, I would use arrayOne[0], since the first element has an index of 0. That is what the i does. It references each index of the array, which is used to get the value at that index. The same goes for the j in the nested loop.


Now, onto your second question:
The reason we use .length is to do with when we want the loop to stop. This is the condition that, while true, will cause the loop to continue running. This condition is always put second in the loop:

for (let someVar = 0; someVar < condition1; someVar++){
                 ^               ^               ^
                 |               |               |
makes the iterating variable
                                 |               |
      used to tell the loop when to stop
                                                 |
used to make the loop iterate until it reaches the condition makes it stop

In this case, we don’t want the loop to iterate past the length of bobsFollowers, as there won’t be anything to iterate over (the array will have ended). So, here, we use .length to tell the loop "when the iterating variable (i)'s value is the same as the length of the array bobsFollowers, stop looping.


Here are a few links to some further reading:

  1. Mozilla Docs
  2. Do factory
  3. Code burst
  4. Geeks for Geeks

I hope this helps!

2 Likes

@codeneutrino has great explanations. I would just like to add that when you use the .length property to determine when the loop ends this will work no matter how long the array is. If you were to add some names to the bobsFollowers array then the loop would still work correctly this way. Had we just designed it to loop 4 times then the code would no longer work properly if the list becomes longer.

It may be a good idea to practice building non-nested loops a few times (either in a code editor on your computer, online code editor or in the dev-tools console). Make sure you are really comfortable with looping through just one array in this way and printing the element at the current counter index to the console. The reason for the nested loops here is that here we are comparing all items from the first array (which has 4 names) to all items from the second array (which has 3 names). So the two loops together will look at 12 combinations (in the case of these two arrays that are provided). These two lists could have 3000 names and 2000 names and the code would work fine as it is written as it would look at all the combinations.

2 Likes

I appreciate your help and for writing it so I understand. I’m finding JS so difficult, maybe because I feel my brain doesn’t work mentally like this. I’m trying though, and won’t give up!

1 Like