Slice and Splice help for challenge

Tell us what’s happening:
Hello everybody, it’s my first post in this community. I’m trying to complete this challenge but i didn’t know why this doesn’t work. Thank you for answer.

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let localVar = arr2;
  localVar.splice(n,n-n,...arr1);
  return localVar;
}
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));

Your browser information:

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

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

What is it that you are not passing? The one about not changing the original arrays?

Anyway, instead of writing n-n you can just write 0, it is an operation less that the code needs to do in this way

If that is the thing you are not passing it is because of the let localVar = arr2 Line, as that is not making a copy of the array, that is creating a different reference to the same array.

To copy an array you need to use slice, concat or the spread operator

1 Like

Thank you, I completed. Now I understand the difference between referencing and copying.