Slice and Splice Challenge #

Tell us what’s happening:
I have currently passed all the tests. But the last test is the problem which says that the second array should remain the same after the function runs. Any help is highly appreciated

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let arr3 = arr2.splice(0,n);
  let one = arr2.slice();
  let arr = arr3.concat(arr1)
  let finalVals = arr.concat(one)
  
  return finalVals;
}


console.log(frankenSplice([1, 2, 3], [4, 5], 1));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:

So this line right here:

let arr3 = arr2.splice(0,n);

… is not doing what you think it does. Take a look at the MDN docs about splice. What you’re doing here, by your syntax, is saying "take the first n elements at the 0 position from arr2, and assign them to arr3.

Splice is a “modify in place” function – it changes the array in its original. So arr2 is being modified directly by calling .splice(...).

1 Like