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