How deep can nested loops go?

did little visual for better understanding nested loops, does we always start from inside, so does it makes sense? thanks

how many levels “deep” can nested loop be? added the 3rd array, and var z, didn’t work out
but might be error in comparing

const myArray = [6, 19, 20];
const yourArray = [19, 81, 2];
const otherArray = [13, 1, 2];

for (let i = 0; i < myArray.length; i++) {
  for (let j = 0; j < yourArray.length; j++) {
     for (let z = 0; z < otherArray.length; z++) {
     }
    if (myArray[i] === yourArray[j] === otherArray[z]) {
      console.log('Both loops have the number: ' + yourArray[j])
    }
  }
};

The variable z only exists within the scope of the loop body, which above is empty.

why is so? “z” is declared the same way as “i” or “j”

Because let gives block scope to variables. They are not accessible (cannot be seen) outside of the block.

switched that: const z = 0, but now I can’t z++; because I can’t change const
anyways, how many levels deep can be nested “for” loop?

let is the way to go when defining a iteration variable (also known as a block parameter). That way its value can change. One never wishes to use const in this role. It cannot be changed so is defeating the purpose.

The issue raised earlier is the empty block, { }. That is the block that z is scoped to meaning the action needs to take place inside this block. Then everything is scoped correctly.


As many as we want, or rather as would satisfy our logistical needs. One wonders how spaghetti like the code would get, though.

For easier dissemination of n-many nested loops we have functions, which can carry out simpler loops.

But for lower strain on resources we have math theorems that present a pattern to follow in terms of efficiency. Might be we don’t need so many nested loops. They are, after all, another degree on N.

If we nest a loop that can possibly iterate to infinity within a loop that may or may not be bounded, we’ve already exceeded the limits of our hardware and will never get a result that makes any sense.

Think of each level as an axis on a multi-dimensional model. Where do you put the fourth dimension? It’s this level of complexity that we expose our code to when we nest loops. Exponential growth means exponential complexity.

2 Likes

it’s alive

  1. I did mistake with the Z scope
  2. there was mistake in comparing expression,
    thanks one more time for your support sir :pray:
const myArray = [1, 2, 19, 20, 500, 10000];
const yourArray = [19, 81, 3, 125, 500, 2, 109];
const otherArray = [13, 1, 2, 990, 100, 500, 3234, 313, 4140, 311];

for (let i = 0; i < myArray.length; i++) {
  for (let j = 0; j < yourArray.length; j++) {
     for (let z = 0; z < otherArray.length; z++) {
        if (yourArray[j] === otherArray[z] &&  otherArray[z] === myArray[i]) {
          console.log("all three arrays have same: "+myArray[i]+" , "+yourArray[j]+" , "+otherArray[z]);
    
     }
    }
  }
};

loop

1 Like

Well done, tackling this. You should know that we are not done, yet, but do go on and revisit this concept later; that is, nested loops. Then again, you may excel at iterators and generators and not need to revisit this discussion, at all. Expect learning opportunities and they will present themselves. We’ve touched upon a very important concept that one feels you will walk away with from this discussion.

What if we were to compare just two arrays and accumulate a matchList. Then compare the matchList to the third array. Does this sound logical and feasible?

1 Like

sound way complicated…yet.
but we have to create new variable “matchList” so it’s gonna add more complexity to the structure

       let  matchList = (myArray[i] === otherArray[z]);
       if (matchList === otherArray[z]){
          console.log(otherArray[z]);
       }

that didn’t worked out quite well

let matchList = []
for (let i = 0; i < myArray.length; i++) {
    for (let j = 0; j < yourArray.length; j++) {
        if (myArray[i] === yourArray[j]) {
            matchList.push(myArray[i])
        }
    }
}
let finalList = []
for (let i = 0; i < matchList.length; i++) {
    for (let j = 0; jj < otherArray.length; j++) {
        if (matchList[i] === otherArray[j]) {
            finalList.push(matchList[i]);
        }
    }
}
matchList = null   // free up memory
if (finalList.length > 0) {
    console.log("All three arrays have the following in common:");
    for (let i = 0; i < finalList.length; i++) {
        console.log(finalList[i]);
    }
}

it looks like more work but breaks the problem down into three steps. The printing is reserved to the last step, and not in the search loops.

When you get to the unit on iterators you’ll learn other ways to break down this problem that are a lot more streamlined, but i don’t want to jump the queue so that will have to wait until we cross that bridge. Be sure to re-visit this problem when you’ve finished that unit.

1 Like

added to bookmarks, thank for support ! enjoy your weekend

1 Like

You’re welcome; and you have a great weekend, too.

Above, the thing to consider, despite all the code, the match list is going to be relatively short. That means the second loop will be very quick.

It’s this sort of consideration that justifies the extra code, in consecutive structures as opposed to an all-in-one solution. You’ll find this a lot in programming. The size of the code is nothing compared to the size of the resource footprint. As we reason out ways to lessen our program’s overhead we will devise refactored code that follows our paradigm.

From a proof of concept perspective, I’m satisfied there is no simplification needed of the above code. It is as efficient as it needs to be.

Until we re-visit this, cheers! Happy coding.