Slice and Splice getting the result but not passing

Tell us what’s happening:

The console logs all match the expected outputs so I am a little confused. Maybe I’m not thinking broad enough?

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let arr3 = arr2.slice(0,arr2.length);
  arr3.splice(n, 0, [...arr1]);
  return arr3;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

console.log(frankenSplice([1, 2], ["a", "b"], 1));
// ["a", 1, 2, "b"]
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));
//["head", "shoulders", "claw", "tentacle", "knees", "toes"]
console.log(frankenSplice([1, 2, 3], [4, 5], 1));
//[4, 1, 2, 3, 5]

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; 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/

The part of your code above [...arr1] has created an array with the same elements that are in arr1. Basically, you are just making a copy of arr1 and splicing it in at the correct location, instead of splicing in the elements. You are using the spread operator which is on the right track, but you need not surround with .

FYI - Since you are making a copy of the entire arr2 array in the following line, you can simply use slice(), because the default starting index of slice is 0 and omitting a value for the end parameter will still extract through the end of the array (arr2.length).

let arr3 = arr2.slice(0,arr2.length);
1 Like

Cheers. It’s those little things that get me sometimes. I usually had trouble because I didn’t wrap the spread arrays in [] so I took to doing it constantly. Gonna have to pay closer attention to that in the future.