All permutations of a sum I'm attempting to implement an algorithm that finds all subsets that sum to a number n. Here is my attempt so far:

function combos(n, combo, result){
  combo = combo || [];
  result = result || [];

  if(sum(combo) === n) {

    result.push(combo);
    return;
  }

  if(sum(combo) &gt; n) {
    return;
  }

  for(var i = 1; i &lt;=n; i++) {
    var newArr = combo.concat([i]);
    combos(n, newArr, result);
  }

  return result;
}

function sum(array) {
  if(!Array.isArray(array) || array.length === 0){
    return;
  }

  return array.reduce((element, sum) =&gt;{
    return element + sum;
  }, 0); 
}


This gives me all combinations. However, there are some redundant solutions. For example. 

n = 3 gives [ [ 1, 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 3 ] ]. When it should be 
[ [ 1, 1, 1 ], [ 1, 2 ], [ 3 ] ]. 

So my problem is I don't know how to jump out of the recursion and not explore the the full breadth of a particular branch. If that makes sense. Any hints would be appreciated.
Return consistent types

Both functions may return different types. For example combos may return an array or undefined. It would be better to return consistent type from all execution paths.

Input validation

The sum function does some input validation, checking if its parameter is an array. But this is too late. It's better to do input validation as close to where you receive the input as possible.

Also, there's no need to require nonempty input. If the input is empty, your algorithm will correctly return 0 as sum or empty array for combos, and that's all well.

Algorithm

In the current algorithm it's inefficient that the sums of arrays are recomputed every time you add a new element, when you could easily know the sums so far. It would be better to use that knowledge and avoid unnecessary recomputations.

To avoid creating results that are duplicates, your algorithm would need a substantial rewrite, which is off-topic here.