Basic algorithm scripting : slice and splice [solved]

Basic algorithm scripting : slice and splice [solved]
0

#1

Here is my solution of this test :

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

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

Does anybody see mistake in it? I have compared this to the ‘Get A Hint’ and various posts of same topic in this camper forum, i’m pretty sure this is already correct, but the curriculum seems adamant not to let me pass the test.


#2

The problem is that splice returns an array of the items deleted. Since you are adding items, it returns an empty array. The solution is to write the splice function on a separate line from the return statement. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice for more on splice.


#3

Thank you, solved. I tried chaining the splice and splice method in a single line, but it didn’t work :smile:. Well, i guess the shortest solution is three lines afterall.