FAQ: Loops - Nested Loops

just did the exercise and on the final step i get the error message with this code even though the output is:
paul
frank

const bobsFollowers = [‘jim’, ‘paul’,‘frank’, ‘john’];
const tinasFollowers = [‘lucy’,‘frank’,‘paul’];
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 + tinasFollowers[j])
}
}
};

however when i type in the mutual friends in

let mutualFollowers= [‘frank’, ‘paul’]

it outputs and the checkbox ticks indicating i completed the exercise:

frank,paulpaul
frank,paulfrank

i check the forums and see everyone is using .push method so i feel as if im missing something, but i thought the way i did it was the easiest and most straightforward way of solving the problem.

Should we be logging at this point? Or accumulating data?

I got through the exercise but the output repeats one of the values and i’d like to know why.
Can anybody help me ?

Because your .log() is within the loop? So every time there is a match, two things happen:

a value is pushed into the array
the entire array gets logged

I dot know really understand the exercise. Can someone give me a clear explanation for dummies in the most simplified way?


  1. This should let us loop between boob and Tinas followers but in which way?

for (let i = 0; i < bobsFollowers.length; i++) {

for (let j = 0; j < tinasFollowers.length; j++) {


What does this mean?


if (bobsFollowers[i] === tinasFollowers[j]) {

  mutualFollowers.push(bobsFollowers[i]);

console.log (mutualFollowers)

Why I get printed?:





[ ‘Marta’ ]
[ ‘Marta’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]

You could consider what you want to do during a single iteration.

Let’s say you have a follower of bob. What do you want to do with that follower?

I don’t get how the loop can help me to find the common follower, in which way? could you explain to me each line of code and what that line does? I don’t get why I get printed:





[ ‘Marta’ ]
[ ‘Marta’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]
[ ‘Marta’, ‘Sam’ ]

Not really sure also of the .push function in this exercise

If you know one follower of bob, presumably you would compare it to tina’s followers and see if she has that follower too?

If you stop obsessing with code and instead think about what needs to happen you might find it a whole lot easier. You can’t write code before having decided what should happen.

If you’re not sure what some built in function does, google for documentation of it.

Sorry I am a bit lost and new to JS, I understood that but cannot visualize the code - like how the .push function really works in this exercise and why I get print what I printed. I did my googling, I watched my videos but I can’t still understand. I thought I may get help here. I did not want to raise any discussion, I just seek for an answer

Discussing can be very good and helpful :slight_smile:

You need to break these things so you understand them. So what does push do? It pushes an element to the end of the array. You could visualize this by imaging a queue for example.

You might decide that you need to look at each of bob’s followers. This would be a loop, so now you can pull up a reference for what a loop looks like and eventually end up with the ability to print out each of bob’s followers, showing successfully having obtained access to each one.

You’d do something similar for tina’s followers, you’d want to look at all of those too. When would you want to look at them? And where is the code executing at that time? Put that there.

You’d do some comparison. What exactly do you want to compare? Maybe you’d then look up operators and see if there’s something you can use there.

If you decide that you need to add a result to an array, then, that would be an action you can look up how that’s done.


Each of the above are really small problems. They’re smaller than the overall thing, because I had some thoughts on what needs to happen which gives me a reference and gives me the ability to split the problem into smaller problems before I at all consider code. So deciding on the actions comes first, if you reason about your program in terms of actions, then you’ll have a much easier time. So why did you get some particular output? Well you carried out some couple of actions, what actions are those, should those actions lead to this output? If not, were the actions correctly implemented, you could print out information about what was being done to observe whether the right thing was done.

If you for example have some output that you didn’t expect, and you don’t know what actions you carried out, then, you’re pretty lost, and you’re not about to solve that problem. To get back on track you would consider what needed to happen and then compare your code to that. If you don’t know exactly how to read your code and you also don’t know what’s supposed to happen then you leave yourself no way to proceed.

for(bobFollower of bobsFollowers) {
  for(tinaFollower of tinasFollowers) {
    if(bobFollower == tinaFollower) {
      mutualFollowers.push(tinaFollower);
      break;
    }
  }
}

Why is it not accepted?

Because you have to use a for loop, not a for of loop

Yeah, a missed teaching opportunity. The for..of loop is perfectly valid, and a more reasonable approach since we only need to access the values, as stated in the instructions.

1 Like

I try this code in jsconsole.com and i don’t why i get output Array (0) . What is wrong?

// Write your code below

let bobsFollowers = ['Lalo', 'Ma', 'Eze', 'Nure'];
let tinasFollowers = ['Lalo', 'Eze', 'Ayub'];
let mutualFollowers = []

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

you could .log() to inspect your code and narrow down the problem, for example:

for(let i = 0; i < bobsFollowers.lenght; i++) {
   console.log(i)

which tells your loop is not running. so you could log bobsFollowers.lenght for example to see if something might be wrong with your condition.

1 Like

can anyone explain me why we use .length here?

We need to know the size of the array so we can reach every index.