Slice and Splice -- recreated arr2

Tell us what’s happening:
Hi,
this solution does not pass the test, because “The second array should remain the same after the function runs.”
But it is the same. Or it is not?

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let end = [];
  let result = [];
 
  end = arr2.splice(n);
  result = arr2.concat(arr1.concat(end));
  arr2 = arr2.concat(end);
  
  console.log(arr2);
  return result;
  
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

if you look up the splice specification you will see it says that:

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
here’s my ref:
Array.prototype.splice() - JavaScript | MDN

so given that arr2.splice is changing your arr2, and the challenge gave you an error that says “The second array should remain the same…”, I think this gives you all you need to figure out what is wrong and move forward.

1 Like

OK, I will change it. Thanks.

Hi,
why this statement would modify arr2:
end = arr2;
and this would not:
end = arr2.slice();
???

thank very much for your help.