Slice and Splice, my doubt

Tell us what’s happening:
Yes, I cheated and used hints- spoiler, However I tried to tweak a little and see if things work. Splice and slice in the same line did not work whereas below code gave me the same result as challenge is asking for. But the code does not pass the challange. Why? please someone clear my doubt on both cases. Thanks in advance for any input.

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!

  let localArray = arr2.slice(0, 100);
 localArray.splice(n,0,[...arr1]);
  return localArray;
}

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

Your browser information:

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

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

You are inserting a new array inside localArray.
I believe your goal is to add element, not a new array

eg your output for the above example is

[ 'head', 'shoulders', [ 'claw', 'tentacle' ], 'knees', 'toes' ]

Side note:

let localArray = arr2.slice(0, 100);

Why making a slice of 100 elements?
What if an input array has 101 elements?

Hope this helps :+1:

1 Like

Ya initially I tried arr2.slice() in the similar code but later played with it. Sorry I posted later one…lol