Slice and Splice -- Basic Algo. Scripting

Hey ! I’m curious as to why this solution will not work. I personally changed the inputs to match that of the necessary requirements to pass as a solution and to me, the solutions are the same. I checked with the ‘typeof’ function and I tried to see what is so wrong with my solution ! Any feedback and criticism will do, thanks !

function frankenSplice(arr1, arr2, n) {
  let resultArr = arr2.slice(0, arr2.length);
  return resultArr.splice(n,0,...arr1);
}

frankenSplice([1, 2], ["a", "b"], 1);

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

Link to the challenge?

Well, you’re not returning anything.

Sorry I accidentally erased it when I was editting the code ! Hahaha

The return value of .splice() is an array of the deleted items.

1 Like

Return the resulting array. The input arrays should remain the same after the function runs.?

`frankenSplice([1, 2], ["a", "b"], 1)` should return  `["a", 1, 2, "b"]` .?

well they need to return quite allot?

also a ussefull link: Slice and Splice (algorithms)

1 Like

*** OMG I JUST READ YOUR LINK SORRY !! Thanks soooo~~ much !!
Here I am logging the array that will be returned and it directly matches the output to the solutions. I didn’t touch the [2] arrays that are the inputs

function frankenSplice(arr1, arr2, n) {
  let resultArr = arr2.slice(0, arr2.length);
  console.log(resultArr);
  // logs [a,b]
  resultArr.splice(n,0,...arr1);
  console.log(resultArr);
  // logs [a,1,2,b]
  return resultArr;
}

frankenSplice([1, 2], ["a", "b"], 1);

I’m curious as to why this won’t work, if I simply just immediately return after splicing.

function frankenSplice(arr1, arr2, n) {
  let resultArr = arr2.slice(0, arr2.length);
  console.log(resultArr);
  // logs [a,b]
  return resultArr.splice(n,0,...arr1);
  console.log(resultArr);
  // logs [a,1,2,b]
  // return resultArr;
}

It doesn’t work because you are not returning resultArr. You are returning the value returned by the splice method, which is an array of the deleted items.