FAQ: Loops - Nested Loops

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.

1 Like

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

not sure you can modify the arrays.

also, if i do:

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

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

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.


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

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


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?

yes, you did. the value of j never changes

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.

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

1 Like

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.
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]) {
console.log(Our mutual followers are: ${mutualFollowers})
//output: Our mutual followers are: Bobby,Throbby

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]){
//output: [ 'John', 'Emily' ]

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

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.

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.

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

Please, help!!! Why doesn’t work??? Error - Did you write a nested for loop?

let bobsFollowers=['Serg','Jan','Sam','Kate'];
let tinasFollowers=['Lena','Sam','Jan'];
let mutualFollowers=[];

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

when we try to log i:

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

we get nothing. This seems to indicate the loop doesn’t run (doesn’t make any iteration)

so something is wrong with your loop condition. Check your spelling :wink:

1 Like

Hey when you are comparing ‘bobsFollowers[i] === tinasFollowers[j]’
Can you explain what is happening?

e.g. is it going array 0 === array 0? array1===array0? array2===array0?
array3===array0? array1===array1? and so on ???


I don’t know if you got an answer but you spelled “length” as “lenght”

thank you very much !!!