0

So I'm writing a method to find recursively all of the permutations of a list of numbers.

I'm adding the result of each run, which is an number array, to a larger combos array. However, combos is becoming filled with empty arrays like this: [[],[],[]] instead of [[1,2,3],[3,2,1],[2,3,1] ...] but I know the combinations are being generated

Here is what I have:

var combos = [];
permuteHelper([1,2,3],[]);
console.log(combos); //printing blank array or arrays [[],[]]
function permuteHelper(list, combination){

    if(list.length == 0){
        combos.push(combination); //ERROR: combos only holds blank arrays (e.g. [[],[]] )
        console.log(combination); //although this is printing correct list after each recursive run (e.g. [3,2,1] )
    }
    for(var i = 0; i < list.length; i++ ){

        //pick a digit
        var digit = list[i];
        list.splice(i,1); //remove the character from the array
        combination.push(digit);

        //recursively keep picking
        permuteHelper(list, combination);

        //backtrack and put the digit back for next time
        list.splice(i,0,digit);
        combination.pop();
    }

}

I've tried making combos non-global and updated the function header

function permuteHelper(list, combination,combos)

but combos is still not populated correctly. I'm new to js, not sure what I'm missing.

2
  • can you provide more details particularly what you want your final output to look like and what you are actually getting? Commented Feb 20, 2018 at 17:25
  • Yes! So if I print combos, I would like to get an output like this: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] However, printing combos looks more like this: [ [],[],[],[],[],[]] Commented Feb 20, 2018 at 17:35

1 Answer 1

2

When you get combination as a parameter to your function, it's treated as a reference. Any change to combination will change the original variable. You hand over that reference to each new call of permuteHelper and therefore always modify the original array.

A primitive solution with few code changes would be:

var combos = [];
permuteHelper([1, 2, 3], [], []);

function permuteHelper(list, _combination) {
    var combination = Object.assign([], _combination);
    if (list.length == 0) {
        combos.push(combination); //this only has a blank array of 
        arrays(e.g. [[], []])
        console.log(combination); //prints correct permuted list after each recursive trial (e.g. [3,2,1] )
    }
    for (var i = 0; i < list.length; i++) {

        //pick a digit
        var digit = list[i];
        list.splice(i, 1); //remove the character from the array
        combination.push(digit);

        //recursively keep picking
        permuteHelper(list, combination, combos);

        //backtrack and put the digit back for next time
        list.splice(i, 0, digit);
        combination.pop();
    }
}

Like that, you create a new object combination from the parameter _combination that can be modified as you please.

Sign up to request clarification or add additional context in comments.

4 Comments

But I do want to modify my original array. I want it to hold all of the combinations that were found. Upon each iterative completion, I want to append this new value to it.
Please see my updated answer, it's exactly the same problem, just a different variable.
Awesome! Thank You
Something I noticed that gives a performance boost is to use slice() instead of Object.assign. Especially when running this permute on arrays with primitive types var combination =_combination.slice()

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.