FAQ: Loops - Nested Loops

#47

As far as not being error-free, yes it is incorrect. We cannot use return outside of a function. Remove that keyword and save/run.

#48

Hi I get an error also in the 4th step but I cannot find the mistake.

there is no error in the console but it says Did you write a nested for loop?

Can someone help me to find what I did wrong?

let bobsFollowers = ['Tom','MC','Jack','Sam'];
let tinasFollowers = ['Tom','MC','Paul'];
let mutualFollowers = [];
for (let i = 0; i < bobsFollowers.lenght; i++) {
  for (let j = 0; j < tinasFollowers.lenght; j++) {
   if (bobsFollowers[i] === tinasFollowers[j])   {
    mutualFollowers.push(bobsFollowers[i]);
    }
  }
};
#49

not sure you can modify the arrays.

also, if i do:

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

i don’t get any output, indicating a problem with your loop

#50

Hi all,

For some reason, when I run my code below, it only prints to the console an empty array “

Could someone tell me why I can’t print to the console the names of the matching followers? I’ve looked all over the forums and I can’t figure out what’s incorrect in my code.

Thanks.

const bobsFollowers = ['John', 'Josh', 'James', 'Jacob'];
const tinasFollowers = ['Sarah', 'John', 'Jacob'];
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(mutualFollowers)
    }
  }
};
#51

.push is a method, calling a method requires parentheses, not square brackets

#52

Help!
I’m stuck at step 4. Every time I ran my code, the page just keeps loading, forever. I can’t get feedback and I can’t move forward. Is there something wrong with my code? Did I write an infinite loop or something?

#53

yes, you did. the value of j never changes

#54

Thank you thank you thank you! Can’t believe that I got stuck on this for 2 hours just because I took i as j. I have to be more careful next time.

#55

or learn how to use breakpoints, that can be really useful in cases like this

1 Like
#56

One thing that is somewhat annoying and will throw an error in completing this task is that the software for codecademy wants you to use the same variables (i and j) which the code doesn’t need to run it properly (I used b and t, for bob and tina)

But for everyone asking how to log the names and not the numbers… anytime you want to reference the data that is stored in the array and not just the index number make sure you do a separate console.log(). there is no need to log every iteration of the loop but if you wanted it to do so… make it a separate command in the codeblock or do it after the loop has completed and put it at the very end of the code that needs to run.
e.g.
let bobsFollowers = [‘Bobby’, ‘Robby’, ‘Throbby’, ‘Moe’];
let tinasFollowers = [‘Bobby’, ‘Steve’, ‘Throbby’];
let mutualFollowers =;
for (b = 0; b < bobsFollowers.length; b++){
for (t = 0; t < tinasFollowers.length; t++) {
if (bobsFollowers[b] === tinasFollowers[t]) {
mutualFollowers.push(tinasFollowers[t])
}
}
}
console.log(Our mutual followers are: ${mutualFollowers})
//output: Our mutual followers are: Bobby,Throbby

#57

When I run this in a separate compiler (Atom: Script) I get the correct value ([ ‘Emily’, ‘Matt’ ]) but for some reason I am getting the incorrect return in this environment. Another question I had is in regards to variable declaration, please notice my commented out line 6, is it best practice to always use inline declaration in js or can I clean my code by declaring variables like this and then assigning their values in line later? Thank you for your help and attention.

// Write your code below
const bobsFollowers = ['John','Jane','Emily','Matt'];
const tinasFollowers = ['Emily','Tony','Matt'];
let mutualFollowers = [];

//let i, j;
//will iterate thru 4 times then stop b/c 4 !< 5
for(let i = 0; i < bobsFollowers.length; i++){
  //iterates thru 3 times then jumps out to other loop to iterate and repeat
  for(let j = 0; j < tinasFollowers.length; j++){
    if(bobsFollowers[i] === tinasFollowers[j]){
      mutualFollowers.push(bobsFollowers[j]);
    }
  }
};
console.log(mutualFollowers);
//output: [ 'John', 'Emily' ]
#58

To conform to normally recognized standards you should use the code as you have written ie.

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

You could use:

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

But it’s kinda unconventional. In the end, it’s your code, and you can do what you prefer. As long as you’re consistent, you and others will be able to decipher your code. If you’re looking to get hired as a developer, you’ll probably be required to follow established standards, so in my opinion, I would conform. Hope this helps!

1 Like
#59

Thanks for your help I’ll start to use inline declarations, as for the code fix I somehow have it typed up correctly in my other compiler but not in CodeAcademy’s lol thanks again for the assist.

#60

I coundn’t pass the first test just simply because I didn’t initilize (i) with a let expression and the funny thing about it is that my code ran flawlessly but the test kept asking me if I’ve had used a for loop.

Can someone tell me if defining variables in loops must come with a keyword expression or not?
Thanks you very much.

#61

Must? No. Should? Yes. When you declare a variable without using var, const or let, JavaScript considers it an undeclared global variable. Do you really want to give global scope to a simple index variable in a for loop? You can if you want, but that practice could lead to problems with large complex projects.
See this for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Declaring_variables