title: How to convert this combination sum algorithm from java to javascript
body: <p>Here is the <a href="https://leetcode.com/problems/combination-sum" rel="nofollow noreferrer">leetcode</a> question, for combination sum problem.</p>
  
  <p>One answer <a href="https://leetcode.com/problems/combination-sum/discuss/16509/Iterative-Java-DP-solution" rel="nofollow noreferrer">code</a> is here in java. (or see follow)</p>
  
  <pre><code>public class Solution {
      public List&lt;List&lt;Integer&gt;&gt; combinationSum(int[] cands, int t) {
          Arrays.sort(cands); // sort candidates to try them in asc order
          List&lt;List&lt;List&lt;Integer&gt;&gt;&gt; dp = new ArrayList&lt;&gt;();
          for (int i = 1; i &lt;= t; i++) { // run through all targets from 1 to t
              List&lt;List&lt;Integer&gt;&gt; newList = new ArrayList(); // combs for curr i
              // run through all candidates &lt;= i
              for (int j = 0; j &lt; cands.length &amp;&amp; cands[j] &lt;= i; j++) {
                  // special case when curr target is equal to curr candidate
                  if (i == cands[j]) newList.add(Arrays.asList(cands[j]));
                  // if current candidate is less than the target use prev results
                  else for (List&lt;Integer&gt; l : dp.get(i-cands[j]-1)) {
                      if (cands[j] &lt;= l.get(0)) {
                          List cl = new ArrayList&lt;&gt;();
                          cl.add(cands[j]); cl.addAll(l);
                          newList.add(cl);
                      }
                  }
              }
              dp.add(newList);
          }
          return dp.get(t-1);
      }
  }
  </code></pre>
  
  <p>I need to convert it in javascript.</p>
  
  <p>Here is my attempt.</p>
  
  <pre><code>function sortFunc(a, b) {
      return a-b;
  }
  
  function combinationSum(cands, t) {
      cands.sort(sortFunc);
  
      let dp = []; //[[[]]];
  
      for (let i = 1; i &lt;= t; i++) {
  
          console.log('-- i --');
          console.log(i);
  
          let newList = []; // [[]];
  
          for (let j = 0; j &lt; cands.length &amp;&amp; cands[j] &lt;= i; j++)
          {
              console.log('-- j --');
              console.log(j);
  
              if (i === cands[j]) {
  
                  console.log('-- push --');
                  console.log(i);
  
                  newList.push([cands[j]]);
              }
              else {
                  // list of int
                  let myListList = dp[i-cands[j]-1];
  
                  for(let k=0; k&lt;myListList.length; k++) {
                      let myList = myListList;
  
                      if(cands[j] &lt;= myList[0]) {
                          myListList.unshift([cands[j]]);
                          newList.push(myListList);
                      }
                  }
              }
          }
  
          dp.push(newList);
      }
  
      return dp[t-1];
  }
  
  let arr = [2, 3, 5];
  let t = 15;
  let out = combinationSum(arr, t);
  console.log(out);
  </code></pre>
  
  <p>I have some understanding of the code, but not a lot. Currently, my javascript is in infinite loop. </p>
  
  <p>Does anyone know why?
  Or if you have a better solution for "combination sum"?</p>