FAQ: Why can't I console log mutualfollowers.push()

My eyes missing to find syntax error no matter how many times I check please if you could tell me what’s wrong with this code.

It would help if we could read the error type.

1 Like

i get through it. It was syntax error. Thank you

1 Like

This should work:

const bobsFollowers = [‘Lars’,‘Connie’,‘Lea’,‘Laurent’];

const tinasFollowers = [‘Laurent’,‘Connie’,‘Esmeralda’];

const 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);

//expected output:[ ‘Connie’, ‘Laurent’ ]

Thank you. It worked in cycles, I put i and j .
Here’s what I added:
mutualFollowers.push(tinasFollowers[j])

I have run this code endless times but I’m not sure why it doesn’t accept it… The console shows no errors and I even tried logging it and it was correct. Is there anything I missed?

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

It may come down to pattern mismatch. The tester may not be expecting white space on the increment expressions.

i++  vs.  i ++
1 Like

Thank you!! That was it. I thought those spaces didn’t matter and it was up to me whether to include them or not.

1 Like

:arrow_upper_left:
Guys, I really dont get this line

I do get why we use mutualFollowers.push() but
I dont understand why we input tinasFollowers[j] or bobsFollowers[i] ?
Why exactly that? We are looking for mutual followers from both array, but then only one of them is being added to the () ? :sweat:
Just want to understand the logic of it…
Initially I wrote mutualFollowers.push() on its own and could not figure out what to add into () until I looked here…

If they are both the same, should it matter which one we push to the array? We don’t want to push both since we want a list of unique followers common to both lists.

As for .push() it takes at least one argument. We wouldn’t want to push nothing, now would we?

If someone already posted this then excuse my redundancy, but this is the best I could come up with to get the log to print exactly what I think everyone hoped it would:

const bobsFollowers = [‘Camile’, ‘Jackson’, ‘Andre’, ‘Dom’];
const tinasFollowers = [‘Jen’, ‘Dom’, ‘Andre’];

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('Both are friends with ’ + tinasFollowers[j] + ‘.’)
}
}
};

console.log(mutualFollowers.join(’ '));

Which logs:

Both are friends with Andre. Both are friends with Dom.

Let me know if there is a better way to get this result.

1 Like

It was ‘correct enough’ to go to the next step but actually it just printed ‘1’ and ‘2’ instead of the names.
When I didn’t wrap it in console.log but put console.log in a seperate line afterwards it perfectly worked… Just the names.
I’m wondering why it showed me that this solution was correct then…

It’s been two days and i’m still trying to figure out the logic behind the following code :
Could someone tell me why we get different outputs when we console.log(mutualFollowers) on different levels of our code blocks? Apologize if this question has already been answered.

This was confusing to me still. I don’t understand why the results weren’t logged into the console and why the .push method was used in this case. I forget sometimes that Codecademy will bring previous lessons and incorporate them into the latest lesson I’m working on.

The push method is used only to add an element to an array. It also returns the length of the array which in this case was unnecessary. The reason we wanted to add the elements that matched to an array is because if you just log it to the console it is lost. If it is in an array tied to a variable you have it throughout the whole program to reference it whenever necessary.

1 Like

2 posts were split to a new topic: Question on push.mutualFollowers