title: JavaScript - How to create variable nested loops with recursion?
body: <p>I'm trying to write a N number of nested loops with recursion, but it has taken me too long to achieve it. I haven´t resolved how to compare all the levels of the array which contains the index of the loops. My goal is to make the indexes array to walk through all the combinations.</p>
  
  <p>For example, if N is 3 then the nested loops with iterations will look like:</p>
  
  <pre><code>var i = 10;
  while (i--) {
      var j = 10;
      while (j--) {
          var k = 10;
          while (k--) {
              if (i === 0 &amp;&amp; j === 0 &amp;&amp; k === 0) { return 0; }
          }
      }
  }
  </code></pre>
  
  <p>and the combinations between i, j, k go from <code>[9, 9, 9]</code> to <code>[0, 0, 0]</code>.</p>
  
  <p>My attempt is this:</p>
  
  <pre><code>function nloops(n) {
      loop(n, [], 0);
  }
  
  function loop(n, array, index) {
      if(array.length != n) {
          array[index] = 10 - 1;
          loop(n, array, index + 1);
      } else {
          if ((n - index + 1) &lt; 0) {
  
          } else {
              if (array[n - index + 1] &gt; 0) { 
                  array[n - index + 1]--; loop(n, array, index + 1);
              }
          }
      }
  }
  
  nloops(3);
  </code></pre>
  
  <p>My expected behaviour would be the array to walk from <code>[9, 9, 9]</code> down to <code>[0, 0, 0]</code>.</p>