Doubts in nested loops exercise

Good afternoon everyone,
I was trying to learn about nested loop , and in one excersice i get stuck and ask for the solution, the problem it’s that i’m not agreed with the solution. I’m new here but i found this could be a interesting way to understand my mistake.
This was the exercise:

Create a nested loop that iterates through bobsFollowers as the array for the outer loop, and tinasFollowers as the array for the inner array. If the current element from the outer loop is the same as the current element from the inner loop, push the that element into the mutualFollowers array.

So the steps before ask two create three variables, and them put in the mutualFollowers the name’s that are in both lists. This was the automatic resolution:

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]);
  console.log(mutualFollowers);
    }
  }
};
// the output was 
[ 'Marta' ]
[ 'Marta', 'Sam' ] 

What i did, and the exercise wasn’t correct was:

const bobsFollowers = ['Joe', 'Marta', 'Sam', 'Erin'];
const 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 = (bobsFollowers[i]);
  console.log(mutualFollowers);
    }
  }
};

// outuput :
Marta
Sam

and my answer wasn’t accept. So i don’t understand why, can you help me?
link:https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-iii?action=resume_content_item

1 Like

Please post a link to the exercise so we can test your code.

1 Like

https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-iii?action=resume_content_item thank you.

2 Likes

Was that link already there in the opening post? If so, my bad for not spotting it.

An empty list is, [], with no content, not even an empty string.

Your function should be logging the list. I’m not a big fan of printing inside a loop (intermediate results) and would rather just construct the list in the loop, and log it outside of (after) the loop.

mutualFollowers = (bobsFollowers[i]);

should be,

mutualFollowers.push(bobsFollowers[i]);
1 Like

I think your code wasn’t wrong if it logged Marta and sam to the console, and I don’t see any problems with your code. I think it’s because how codecademy testes files. It needs to be the same even if yours work

1 Like

Nothing of the sort. The expected result is an array with two names in it. The OP code should not pass since it does not do that.

1 Like

OK thanks. Good to know :grinning:

2 Likes

I’m also struggling with this exercise, but it has taught me to look up and research more on nested loops. I eventually figured out what my issue was due to this forum post, so thanks for that.

Suggestion to possibly add some more material on nested loops involving arrays in the future? (apology if I am missing that content or it’s later in the pro cademy experience. )

No reply needed if you get this notification on Sunday…happy to wait until Monday :slight_smile:

When we look at a two dimension list we see that it has a list on each row of the outer list. We need one loop to iterate over the outer list (the rows) and one loop to iterate over the columns (the rows of the inner list). Say we have a list composed of lists of numbers and we want the sum total of all the numbers.

a = [
      [2, 5, 7, 11],
      [13, 17, 23, 29],
      [5, 31, 37, 41],
      [7, 43, 47, 53]
]

First we need the sum of each row, then the sum of those sums.

total = 0
for row in a:
    row_total = 0
    for item in row:
        row_total += item
    total += row_total
print (total)    # 371

Okay, I see where you are going with this. Thanks for the example @mtf

1 Like

You’re welcome @mattbroski. You’ll find there are several ways to refine the above naive algorithm but one should master this and use it as a starting point with each problem. We never want to forget this part of our learning so keeping it as a basis for other refinements (refactoring) keeps it close to the surface.

For snickers and giggles,

>>> sum([sum(x) for x in a])
371
>>> 

These will come up soon if they haven’t already.

  • sum() built-in function
  • list comprehension
  • iterators in general

And more to the idea of nested loops…

>>> sum([y for x in a for y in x])
371
>>>