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
220k42 gold badges245 silver badges336 bronze badges
asked Oct 26, 2021 at 12:31
6
  • 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. Commented Oct 26, 2021 at 12:35
  • @RandyCasburn did you very your claim? Commented Oct 26, 2021 at 12:38
  • @RandyCasburn I removed all the unnecessary code, it is still not working. Commented Oct 26, 2021 at 12:39
  • 1
    @t.niese - the OP edited the question after I posted my comment. Commented Oct 26, 2021 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. Commented Oct 26, 2021 at 12:42

1 Answer 1

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);

answered Oct 26, 2021 at 12:46

3 Comments

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 yes objects (not primitives) in javascript behave in many ways similar to pointers in c++, but not exactly the same.
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.