title: How to sum of each array combination using javascript?
body: <p>I need to Find the Sum of each array combination &amp; required each combination in each line.<br />
  How to get the sum of each combination and display it in each line with innerHTML ?</p>
  <p><strong>EDIT</strong></p>
  <p>For each number in the range of 0 to 9999, get the sum of their digits: ex. <code>123 =&gt; 1 + 2 + 3 = 6</code></p>
  <p>Display each line with a zero padded 4 digit index number, an equal sign, and the sum: ex. <code>0123 = 6</code></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arraysToCombine = [
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  ];
  var getAllCombinations = function(arraysToCombine) {
    var divisors = [];
    for (var i = arraysToCombine.length - 1; i &gt;= 0; i--) {
      divisors[i] = divisors[i + 1] ? divisors[i + 1] * arraysToCombine[i + 1].length : 1;
  
    }
  
    function getPermutation(n, arraysToCombine) {
      var result = [],
        curArray;
      for (var i = 0; i &lt; arraysToCombine.length; i++) {
        curArray = arraysToCombine[i];
        result.push(curArray[Math.floor(n / divisors[i]) % curArray.length]);
      }
      return result;
    }
    var numPerms = arraysToCombine[0].length;
    for (var i = 1; i &lt; arraysToCombine.length; i++) {
      numPerms *= arraysToCombine[i].length;
    }
    var combinations = [];
    for (var i = 0; i &lt; numPerms; i++) {
      combinations.push(getPermutation(i, arraysToCombine));
    }
    return combinations;
  }
  document.write(getAllCombinations(arraysToCombine));</code></pre>
  </div>
  </div>
  </p>