Nested loops

#1
// Write your code below
let bobsFollowers = ['Pako', 'Loco', 'Taco','Choco'];
let tinasFollowers = ['Taco', 'Bread', 'Pako'];

let mutualFollowers = []

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

Please help me, I can’t find the error here. There is nothing in the output.

FAQ: Loops - Nested Loops
#2

lets inspect the loops by inserting a console.log statement:

// Write your code below
let bobsFollowers = ['Pako', 'Loco', 'Taco','Choco'];
let tinasFollowers = ['Taco', 'Bread', 'Pako'];

let mutualFollowers = []

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

oops, i don’t see an i get here message, so your loops aren’t doing anything. What could be wrong?

1 Like
#3
// Write your code below
let bobsFollowers = ['Pako', 'Loco', 'Taco','Choco'];
let tinasFollowers = ['Taco', 'Bread', 'Pako'];

let mutualFollowers = []

for (let i = 0; i < bobsFollowers; i++) {
  console.log('Hello?')
  for (let j = 0; j < tinasFollowers; j++) {
    console.log('It Works?')
    if (bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers.push(tinasFollowers[j])
    } 
  }
};
console.log(mutualFollowers);

Hmm, is it because of the 0?

#4

no, because 0 is the starting index, you want that.

and i++ is also fine, you want to increase of steps by 1

so that only leaves the loop condition.

1 Like
#5

Ahh so i need the .length cuz of it transfer the stings into numbers?

#6

bobsFollowers and tinasFollowers are not strings. The length property of an object which is an instance of type Array sets or returns the number of elements in that array.

#7

Sorry, meant the Strings in the array.

#8

we want the length of the arrays, given that is what we are looping over

1 Like
closed #9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.