Struggling with Slice and Splice

Tell us what’s happening:

Your code so far


function frankenSplice(arr1, arr2, n) {
      for(let i = 0; i < arr1.length; i++) { 
          arr2.splice(n, 0, arr1[0]);
          arr2.splice(n+1, 0, arr1[1]);   
      }
      return arr2;
}

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

Hi ncampbell89,

You are almost there!
You only need 1 splice (delete the first splice). change 1 to i and you should be good to go.

Happy coding!

I’ve tried that but the first array came out backwards. I’ll show you.

function frankenSplice(arr1, arr2, n) {
for(let i = 0; i < arr1.length; i++) {
arr2.splice(n, 0, arr1[i]);
}
return arr2;
}

frankenSplice([“claw”, “tentacle”], [“head”, “shoulders”, “knees”, “toes”], 2);

It came out as [ ‘head’, ‘shoulders’, ‘tentacle’, ‘claw’, ‘knees’, ‘toes’ ]. It supposed to be [ ‘head’, ‘shoulders’, ‘claw’, ‘tentacle’, ‘knees’, ‘toes’ ]

You’re n is missing a +i.

Also, to avoid arr2 getting modified.

Use this

let newArr = arr2.slice();


newArr.splice  ....

return newArr;