2

Suppose I have this pattern:

for(let i =0; i < 3; i++){
  for(let j =0; j < 3; j++){
    for(let k =0; k < 3; k++){
      console.log(i,j,k)
    }
  }
}

Benefit is I have access to all iteration variables within innermost loop. i, j, and k, disadvantage is it is hardcoded to 3 levels of nested loops. If I want a fourth I need to add that code manually.

I am trying to generalize the code using recursion where I can set the number of nested loops to an arbitrary variable. Here is what I am trying:

const maxNestedLoops = 3;
const iterations = 3;

const indexes = [];

function looper(loopNumber){  
  for(indexes[loopNumber] = 0; indexes[loopNumber] < iterations; indexes[loopNumber]++){

     if(loopNumber < maxNestedLoops){
      looper(loopNumber + 1);
     }
      console.log(indexes);

  }
}

looper(0);

The first results in the following in the console:

0, 0, 0
0, 0, 1
0, 0, 2
0, 1, 0
0, 1, 1
0, 1, 2
0, 2, 0
0, 2, 1
0, 2, 2
...and so on

However with my recursive function example it is not the same:

[0, 0, 0, 0]
[0, 0, 0, 1]
[0, 0, 0, 2]
[0, 0, 0, 3]
[0, 0, 1, 0]
[0, 0, 1, 1]
[0, 0, 1, 2]
[0, 0, 1, 3]
[0, 0, 2, 0]
[0, 0, 2, 1]
[0, 0, 2, 2]
[0, 0, 2, 3]
[0, 0, 3, 3]
...and so on

Problems are that not only are there four indexes instead of three. But some of the values are 3s and I would expect it to only go up to 2.

Advice appreciated.

3 Answers 3

1

The problem is that:

  • your console.log should only be executed at the deepest level. So put that console.log in an else clause.

  • The base case happens when loopNumber === maxNestedLoops - 1 as that is the last index of your array, so the if condition should correspond to that

     if (loopNumber < maxNestedLoops - 1){
         looper(loopNumber + 1);
     } else {
         console.log(indexes);
     }
Sign up to request clarification or add additional context in comments.

6 Comments

That get's rid of the 3s showing up in the output. But I still .length of 4 on that indexes array when I think I should be getting 3
Try return looper(... instead of looper(...
indexes have to be 4, because it depends of number of loops
WillD, for that you should change the test, as indexes run to the array size - 1. Updated answer.
If the loopNumber is maxNestedLoops - 1 (in this example 2), you run the function again with increased loopNumber. That means you run it with loopNumber = 3 and this is extending the array to length of 4 (since indexes[3] points to forth element in array).
|
1

There is problem with the for loop. indexes[loopNumber]++ will be increased until it will reach the value of iterations. Since your value of iterations is 3, you will end up with values which are equal to 3 in the indexes array, because the loop itself is modifying the array.

Also, since you modify your array if indexes before checking if the loopNumber reached the number of maxNestedLoops, you will end up with an array with length of maxNestedLoops + 1.

What I suggest you should do:

const maxNestedLoops = 3;
const iterations = 3;

const indexes = [];

function looper(loopNumber) {
  // Check if we reached the number of nested loops before doing anything else.
  if (loopNumber < maxNestedLoops) {
    // Don't modify the indexes array directly, use a variable instead
    for (let i = 0; i < iterations; i++) {
      indexes[loopNumber] = i;
      looper(loopNumber + 1);
      console.log(indexes);
    }
  }
}

looper(0);

Comments

1

Here you go. It was a interesting one :)

const maxNestedLoops = 3;
const iterations = 3;

const indexes = [];

function looper(level){
    for (let i=0; i<iterations; i++){
        indexes.push(i);
        if (level === maxNestedLoops-1) {

            console.log(indexes);

            indexes.splice(level,1);
            continue;
        }

        looper(level + 1);
        indexes.splice(level,1);
    }
}

looper(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.