FAQ: Loops - Nested Loops

Ahh ok, thanks! That’s just for the parsing of answers though right? whitespace doesn’t actually matter in js appart from for neatness)?

Yea, the problem was the validation done by the codecademy exercise.

For JavaScript in particular, I would recommend a linter/prettier to be consistent (especially when working with multiple people on a project)

1 Like

My code is as follows:

const bobsFollowers = ['Abel', 'Benjamin', 'Charles', 'Danny'];
const tinasFollowers = ['Brenda', 'Charles', 'Danny'];
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(tinasFollowers[j]);
      console.log(mutualFollowers);
    }
  }
};

and output is:

[ ‘Charles’ ]
[ ‘Charles’, ‘Danny’ ]

Can’t seem to find a way to ‘mask’ the first output, meaning I would only like to print:

[ ‘Charles’, ‘Danny’ ]

your .log() which causes the output is within the loop, so each time a mutual follower is added, the whole array is printed

if you only want the final result, the .log() should be at the end/after the loops

my code is correct but its not ticking it off, just keeps spinning as if its thinking about it…
anyone else?

could be an infinity loop. Please this topic:

[How-to] Create a topic that everyone will read

what information you can provide so we are better able to help you

its just happened again…

// Write your code below

const cupsOfSugarNeeded = 3

const cupsAdded = 0

do { 

  cupsAdded ++;

} while (cupsAdded < cupsOfSugarNeeded);

Can we reassign to a const?

i reasigned to let and that seemed to work…
let me pass the first 2 tasks as const but not the third. why?

It mightn’t have been until then that there was any reassignment. Watch for little details like this when looking for problems. Always a good idea to check as you go to catch this sort of error.

I’m not sure what I’m doing wrong, but after i run the code, I just get an error message saying: /home/ccuser/.bin/node: line 3: 123 Killed /usr/bin/node --max-old-space-size=200 $@

This is my code:

let bobsFollowers = ['may', 'marry', 'ahmed', 'bob'];
let tinasFollowers = ['dia', 'bob', 'may'];
let mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++){
  for (let j = 0; i < tinasFollowers.length; j++){
    if (bobsFollowers[i] === tinasFollowers[j]){
      mutualFollowers.push(bobsFollowers[i]);
    }
  }
}
console.log(mutualFollowers);

Seems your program was killed/terminated because it took to long to complete. Which would indicate an infinity loop. Which you have, this loop:

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

will never end.

Thank you! I changed it and it works now.

Can someone help what is wrong in the below code ?

// Write your code below

let bobsFollowers = ["Tamesh","Raj","Papan","Holi"];

let tinasFollowers = ["Tamesh","bhoot","Raj"];

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

    }

  }

}

The exercise is being annoying, its not allowed to have a space between for( and let. Seems validation (from codecademy side) uses some sort of regex without much toleration for certain things

let bobsFollowers = ['James', 'Jane', 'Jimmy', 'John']
let tinasFollowers = ['Jane', 'John', 'Tommy']
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)

What is the difference between using let or omitting let when declaring the iterator? It seems that the code will run without error if I do not declare the iterator with let. Does let really serve a purpose in the for-loop in certain circumstances?

LET defines block scope so the variable is confined to the loop code block it is declared in. It cannot leak into the scope outside of the loop body.

Defining scope is a vital concern to prevent our variables from polluting the global scope where collisions can occur. It is important to keep learning as much as you can about scope in JavaScript. It is a going concern in all programs.

1 Like

Thanks for the quick feedback. Now I know, and knowing is half the battle.

2 Likes

wow, up until now I had no issues with the validation of solutions.
But seems I had the same problem, after removing the space it now passes.
:man_facepalming:

Thank you!

Hello! I was able to execute this lesson successfully however I don’t fully understand it. I don’t understand how the code below means that the mutual friends are found. Does 'bobsFollowers[i] and tinasFollowers[j] take on the value for the friends they have in common?

if (bobsFollowers[i] === tinasFollowers[j]) {
mutualFollowers.push(tinasFollowers[j]);

thanks!