Slice and Splice Exercise - I am almost there

The instructions to solve this exercise are:
"You are given two arrays and an index.

Copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

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

This is the code I wrote so far, I am almost there

function frankenSplice(arr1, arr2, n) {
  
  let sumOfArrays = arr2.slice() + arr1.slice();

  if (arr2 += arr1.splice(n)) {
    return [sumOfArrays];
  }
}

console.log(frankenSplice([1, 2, 3], [4, 5], 1));

The output is [ '4,51,2,3' ]
while the correct output should be [4, 1, 2, 3, 5]

Some indications on how to solve this exercise?

in my result a comma is missing between arr2 and arr1
and arr1 should be copied into arr2 (exactly in the middle of arr2 )

First, please always supply a link to the challenge. You’ve been around here long enough to know that. :wink:

arr2 += arr1.splice(n)

I’m not sure what you think that is doing, but I would say that whatever you think that it doing, it is not doing that.

As a hint, how many elements are in that array? Of what type?

here is the link to the exercise https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

1 Like

Without giving away the answer, one way to approach this is to break up the final product into a sequence of three parts: arr2 [fragment] then arr1 [whole] then arr2 [fragment].

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.