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

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

This is what I did:
console.log(${mutualFollowers})
you’ve used the ‘push’ function, but variable mutualFollowers still remains the same.

Hey mate,

It’s relational to “Scope (and general variables)” one of the topics of this course. It’s not the same logging to the console a value inside of an outer or inner loop or logging it in the ‘general code’. Here I share with you the link to the Codecademy official cheatsheet where you can read this in detail: https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-scope/cheatsheet

Hope I solved your doubts :raised_hands:

Your printing where they are similar from the arrays your comparing. You should use mutualFollowers.push(bobsFollowers[i])

try =
console.log(mutualFollowers.join())
think this might be what you are after

let bobsFollowers = [‘Ellie’, ‘Frankie’, ‘Val’, ‘Dough’];
let tinasFollowers = [‘Angie’, ‘Ellie’, ‘Frankie’];
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);

How true is this statement…
if (bobsFollowers[b] === tinasFollowers[t]) {
mutualFollowers.push(bobsFollowers[b]);
}
shouldn’t the output for this (bobsFollowers[b] === tinasFollowers[t]) be different, considering they both would output different strings and not the same strings

Here is my code:
// Write your code below
let tinasFollowers = [" Nada", " Gerlu", " Misha"];
let bobsFollowers = [" Nada", " Gerlu", " Noso", " Kouwe"];
let mutualFollowers = ;

for(let i = 0; i < bobsFollowers.length; i++){
for(let x = 0; x < tinasFollowers.length; x++){
if(tinasFollowers === bobsFollowers[i]){
mutualFollowers.push(bobsFollowers[i]);
}
}
}
console.log(Mutual friends are: ${mutualFollowers}); // Mutual friends are: Nada, Gerlu

1 Like

Try this.

// Write your code below const bobsFollowers = ["Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Aaryn", "Aayan", "Aazaan"]; const tinasFollowers = ["Alum", "Alvern", "Alvin", "Alyas", "Amaan", "Aman", "Amani", "Aarez", "Aarman", "Aaron"]; let mutualFollowers=[]; /* for (let i = 0; i < bobsFollowers.length; i++){ console.log('These are Bobs Followers ' + bobsFollowers[i]); //console.log(bobsFollowers[i]); } for (let j = 0; j < tinasFollowers.length; j++){ console.log('These are Tinas Followers ' + tinasFollowers[j]); //console.log(tinasFollowers[j]); } */ for (let i = 0; i < bobsFollowers.length; i++){ for (let j = 0; j < tinasFollowers.length; j++){ //console.log(bobsFollowers[i] + " " + tinasFollowers[j]); if(bobsFollowers[i] === tinasFollowers[j]){ //console.log(tinasFollowers[j]); // mutualFollowers.pop(); mutualFollowers.push(tinasFollowers[j]); } } } console.log(mutualFollowers);

let bobsFollowers = [‘Julie’, ‘Tom’, ‘Alexis’, ‘Kaitlyn’]
let tinasFollowers = [‘Bill’, ‘Tom’,‘Kaitlyn’]
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);

hey, this is my code, and I’m pretty sure it is correct. But it’s showing an error like this " Did you append elements to mutualFollowers in the nested for -loop? Ensure you’re not creating it manually!’

Can you wrap your code blocks in three back-tics to ensure it remains properly formatted in the forum posts?

Otherwise, this looks OK At least I don’t see any issues and it runs for me. Sometimes the code validator can be a bit picky. You can try declaring your variables with ‘const’ and adding some white space to your ‘i’ and ‘j’ iterator assignments.

Sorry about the format. But I finally figured out what was wrong. It turned out that I shouldn’t left left space between the square brackets in let mutualFollowers = . I removed the space, and everything was working. :sweat_smile:

1 Like

Here is my Code:

const bobsFollowers = [‘Maja’, ‘Marija’, ‘Ivana’, ‘Tomas’];
const tinasFollowers = [‘Mario’, ‘Ivana’, ‘Maja’];
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(tinasFollowers[j]);
}
}
}
console.log('Mutual followers are: ’ + mutualFollowers); //prints: Mutual followers are: Maja,Ivana

It does happen to all of us. so don’t worry.

I’m also confused, Codecademy has accepted this code, but I don’t understand why it prints the first name and then the two matching names?

const bobsFollowers = ['Clive', 'Dave', 'Abi', 'Wilbur'];
const tinasFollowers = ['Abi', 'Wilbur', 'Steve'];
const mutualFollowers = [];

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

Output is:
[ ‘Abi’ ]
[ ‘Abi’, ‘Wilbur’ ]

Is someone able to explain how to get it to print just the second line showing the two matching names?