0

My code looks like this

var res = [];
var temp = [];

function Permutations(target, size) {
  if (size === 0) {
    res.push(temp);
    console.log(res);
    return;
  }

  for (let i = 0; i < target.length; i++) {
    if (target[i] !== null) {
      temp.push(target[i]);
      target[i] = null;

      Permutations(target, size - 1);

      target[i] = temp.pop();
    }
  }
}

Permutations([1, 2, 3], 2);
console.log(res);

When I run my code, I can see my res stores each permutation as it is is being executed. However, when I log it outside the function, all the stored value disappeared.

[ [ 1, 2 ] ]
[ [ 1, 3 ], [ 1, 3 ] ]
[ [ 2, 1 ], [ 2, 1 ], [ 2, 1 ] ]
[ [ 2, 3 ], [ 2, 3 ], [ 2, 3 ], [ 2, 3 ] ]
[ [ 3, 1 ], [ 3, 1 ], [ 3, 1 ], [ 3, 1 ], [ 3, 1 ] ]
[ [ 3, 2 ], [ 3, 2 ], [ 3, 2 ], [ 3, 2 ], [ 3, 2 ], [ 3, 2 ] ]
[ [], [], [], [], [], [] ]   // This is my console.log outside the function
David
  • 208,112
  • 36
  • 198
  • 279
Kevin
  • 155
  • 9
  • 1
    Because you console.log() the call to the empty version of the function on line 3. Then, redeclare the function on later - that's the one that works. – Randy Casburn Oct 26 '21 at 12:35
  • @RandyCasburn did you very your claim? – t.niese Oct 26 '21 at 12:38
  • @RandyCasburn I removed all the unnecessary code, it is still not working. – Kevin Oct 26 '21 at 12:39
  • 1
    @t.niese - the OP edited the question after I posted my comment. – Randy Casburn Oct 26 '21 at 12:40
  • 3
    @RandyCasburn Yes I know. But your statement was still not true for the old code. Both functions `function Permutations(target, size)` were hoisted, and `Permutations([1, 2, 3], 2);` always called the last one due to that. – t.niese Oct 26 '21 at 12:42
  • Please state your problem clearly. What output are you expecting? There is not a part of your code that does permutations. What are we learning here? Math, JS, recursion or algorithms? – Dropout Oct 26 '21 at 12:48

1 Answers1

1

The array temp holds is the same array throughout the complete execution of your code. And res.push(temp); adds this same array (not a copy of it) to your res array.

Here a related question about how Objects are handled in JavaScript: Is JavaScript a pass-by-reference or pass-by-value language?

So your code results in res having N times the same array.

You could copy the element stored in temp to a new array using [...temp], and push that to your res array.

var res = [];
var temp = [];

function Permutations(target, size) {
  if (size === 0) {
    res.push([...temp]);
    return;
  }

  for (let i = 0; i < target.length; i++) {
    if (target[i] !== null) {
      temp.push(target[i]);
      target[i] = null;

      Permutations(target, size - 1);

      target[i] = temp.pop();
    }
  }
}

Permutations([1, 2, 3], 2);
console.log(res);
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • Oh I see it now. So objects and arrays in JavaScript are pushed as a pointer to the original object. Looks like it is a bit different from C++. Thank you. – Kevin Oct 26 '21 at 12:53
  • @Kevin yes objects (not primitives) in javascript behave in many ways similar to pointers in c++, but not exactly the same. – t.niese Oct 26 '21 at 12:58
  • 1
    Not directly relevant in this example, but it might be worth noting that the spread operator only makes a shallow copy of the array. If the copied array contained nested arrays, these were still linked to the original... which might lead to a similar confusion – Corrl Oct 26 '21 at 14:29