Help with Slice and Splice

Tell us what’s happening:
My code is returning the right answer but its not passing. please help

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  var sliced= arr2.slice();
  
  sliced.splice(n,0,arr1);

  return sliced;
}

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

Your browser information:

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

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

Loop through all of the items in the first array. For each item in the first array splice it into the copied array in the index given as argument.

Increment the index after performing the splice.

It looks like your not looping through the sliced array and incrementing the index! :vulcan_salute:

sliced.splice(n,0,arr1);
1 Like

The instructions tell you to copy each element of arr1 into arr2. Instead you have copied the actual arr1 into arr2. Think back to something you learned in the ES6 section which helps you “spread” out an array into its elements.

2 Likes