Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
The following code is working, and I understand why.

function frankenSplice(arr1, arr2, n) {
let localArr = […arr2];
localArr.splice(n, 0, …arr1);
return localArr;
}

But why is my other solution not working?

Instead of assinging arr2 to a variable I just chain on the splice method immediatly.

But when I do it like this, I get an empty array…

  **Your code so far**

function frankenSplice(arr1, arr2, n) {
return [...arr2].splice(n, 0, ...arr1);
}

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.90 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

you can’t assign to return and that will make your code not execute at all

after that, depends on how you fix it, you may have some other issues

I’m sorry that was a typo. I know I can’t assign to return. I edited the post.

what does splice return? check the documentation if you don’t know

Aaah, I get it now. Thank you!

1 Like

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