Slice and Splice.. can someone help me understand why this is failing the tests?

Tell us what’s happening:
Can someone help me understand why this solution does not work for this challenge? I pass all of the test inputs into it and they come back as they should, however, the webpage keeps telling me outPut is undefined.

Thank you so much!

Your code so far


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

  return outPut;
}

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

Your browser information:

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

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

Firstly, you’re not using a variable keyword (var or let). So outPut = arr2 is trying to assign a global variable, and the JS will just error if you try to do that - that is why the console says

assignment to undeclared variable outPut

Secondly, the task is asking you not to modify the original arrays. outPut = arr2 is not creating a copy, its the same array. You need copy arr2, and assign that copy to outPut

Thank you so much Dan!

That is crystal clear. Sometimes, after banging at the same problem you lose perspective. Thanks again!

1 Like