Slice() and Splice() - Basic algorithm scripting[1]

Tell us what’s happening:
On this particular challenge I am trying to solve how to copy an array into another array and on running the code in the challenge it doesn’t work. what could be the issue. I’ve researched and I’m having trouble finding a more applicable solution
Your code so far


function frankenSplice(arr1, arr2, n) {
  let mArr = arr2.slice();
for (var i = 0; i < arr1.length; i++) {
  return mArr.splice(n, 0, arr1[i]);
}

}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

When the splice method is called on an array, it will return an array with any deleted elements.

mArr.splice(n, 0, arr1[i]);

You have specified it to not delete any items (the second argument passed to the method), so it will always be an empty array that gets returned.

You have another issue in the fact that the return keyword placed before this will cause the function to exit after the very first iteration (where i is equal to 0).

See if you can make some adjustments to your logic and try again.

I can’t quite get removing the zero. won’t it return all the elements in using splice method and return an empty array when using slice() method.

should I focus more on rectifying the for loop…

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