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:
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.
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.
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
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
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.