# FAQ: Loops - Nested Loops

#24

Maybe so, but it won’t be for long. Just keep pouring over the lessons, and reading as you go. Practice each new concept along the way so you can see more of the in’s and out’s. It all may look simple, but there are some hidden jewels in even the simplest of concepts.

#25

This is what I’m trying to do

It’s all well and good me learning something in a lesson, but if i don’t practice and continue to build my knowledge of these concepts i’ll be forever amateur.

1 Like
#26

This is my code:

``````const bobsFollowers = ['Friend 1', 'Friend 2', 'Friend 3', 'Friend 4'];
const tinasFollowers = ['Friend 1', 'Friend Two', 'Friend 3'];
const mutualFollowers = [' '];
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; j < tinasFollowers.length; i++) {
if (bobsFollowers[i] === tinasFollowers[j]) {
console.log(mutualFollowers.push(tinasFollowers[j]))
}
}
};
``````

when i run the program… it just thinks indefinitely and never tells me if i got it correct. I have tried refreshing. Signing out and signing back in. Restarting the computer. Using a different internet connection. It only happens when i insert the loop. Does this mean that the loop is running infinitely? If so, how do i fix it?

#27

yep, you do indeed have an infinity loop.

you need to identify the mistake, there are only two possible lines which could cause an infinity loop.

you could study them? Or run them separately to see which loop is causing you problems, then you narrowed it down to a single line.

#28

Just wondering what I’m doing wrong here?

``````// Write your code below
let bobsFollowers = ["adam", "james", "oliver", "stacy"];
let tinasFollowers = ["olivia", "stacy", "james"];
let mutualFollowers = [];

for (i = 0; i < bobsFollowers.length; i++) {
for (j = 0; j < tinasFollowers.length; j++) {
if (bobsFollowers[i] === tinasFollowers[j]) 			{
mutualFollowers.push(bobsFollowers[i]);
}
}
}
``````

Codecademy asks me whether I’ve used a nested for loop, which I believe I have done.

#29

you have done, if we look at the mutual followers:

``````// Write your code below
let bobsFollowers = ["adam", "james", "oliver", "stacy"];
let tinasFollowers = ["olivia", "stacy", "james"];
let mutualFollowers = [];

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

everything seems fine. Sounds like a bug too me, just submit the code several times, that should display the solution button, use this as work around, knowing your code is fine

1 Like
#31

Hi!

``````const bobsFollowers = ['Jane', 'Izumi', 'Sasha', 'Natsu'];
const tinasFollowers = ['Haru', 'Izumi', 'Natsu'];

const mutualFollowers = [];

for(var i=0; i<bobsFollowers.length; i++){
for(var j=0; j<tinasFollowers.length; j++){
if(bobsFollowers[i] === tinasFollowers[j]){
mutualFollowers.push(bobsFollowers[i]);
console.log('i and j are ' + bobsFollowers[i]);
console.log('The mutual Followers are ' + mutualFollowers);
console.log('The last mutual Follower is ' + mutualFollowers[mutualFollowers.length-1]);

}
else{
console.log('i is '+ bobsFollowers[i]+ ' and j is '+ tinasFollowers[j]);
}
}

}
``````

Is there a way to get the value of an array index without using this trick?

``````console.log('The last mutual Follower is ' + mutualFollowers[mutualFollowers.length-1]);
``````

Thank you!

#32

What’s the reason that my array at the end of this has an undefined entry?

``````// Write your code below
let bobsFollowers = ['a','b','c','d'];
let tinasFollowers = ['a','b','e'];
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.length);
mutualFollowers.push(tinasFollowers[j]);
console.log(mutualFollowers);
}
}
};
``````

My results are:

``````[ 'a' ]
[ 'a', 'b' ]
[ 'a', 'b', undefined ]
``````
#33

It likely has to do with the one list being longer than the other. What happens if you push `bobsFollowers[i]`?

#34

I get the following result:

``````[ 'a', 'b', 'd' ]
``````

#35

Consider the conditionals in your for loops. Is there any reason to write `<=`?

1 Like
#36

Of course it’s an off by 1 error!
Thank you! I’ve got it working and understand now.

1 Like
#37

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.

#39

My code is below. For some reason it only runs once and the output is ‘Ann’, rather than ‘Ann, James’.
Can someone help?

let bobsFollowers = [‘Ann’, ‘James’, ‘Emma’, ‘Ada’];
let tinasFollowers = [‘Ann’, ‘James’, ‘Tim’];
let mutualFollowers = ;

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

#40

See anything amiss? Check the variable increment.

#41

thank you for the quick response! fixed it now

just out of curiosity, if my variable increment was wrong, why was Ann still being logged?

#42

Because it just happened to be at the front of the list.

#44

My code for this exercise is below. I get to step 4 of creating the Nested Loop and I keep getting the error message “Did you accidentally delete your bobsFollowers array?” I haven’t deleted the array or touched it since I first created it in step 1. When I check it against the solution code, it seems to be the same. Am I missing something here?

``````let bobsFollowers = ['Linda', 'Louise', 'Gene', 'Teddy'];
let tinasFollowers = ['Louise', 'Gene', 'Jimmy Jr.'];
let mutualFollowers = [];

for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; j < tinasFollowers.length; j++){
if(bobsFollowers[i] === tinasFollowers[j]){
return mutualFollowers.push(bobsFollowers[i]);
}
}
};
``````
#45

what i find confusing is the lack of output, you should get this error: