Slice and Splice -- recreated arr2

Slice and Splice -- recreated arr2
0

#1

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


#2

As soon as you spliced arr2, you mutated it. You reassigned a value back to arr2, but you created a new reference which is not the same as the original. It may have the same values, but it is not the same array.


#3

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:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

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.


#4

OK, I will change it. Thanks.