Basic Algorithm Scripting: Slice and Splice - Unique process validation?

So in my case I found a little impractical the fact of using an for loop for the operation so I solved the challenge this way:

function frankenSplice(arr1, arr2, n) { 
  return [...arr2.slice(0, n), ...arr1, arr2.slice(n, arr2.length)];
}
frankenSplice([1, 2, 3], [4, 5], 1);

I tried out every requirement and it do return the data as the challenge asks for but it just keeps returning an error in everything but the fact that both arrays must stay untouched.

Is it mandatory to use the For process in order to get the challenge correct?

Almost… your function is actually returning [ 4, 1, 2, 3, [ 5 ] ]

1 Like

Oh my! thanks man!
Guess I’ll start checking my code on jsbin before hand, when I did logs on the challenge page it never shows the brackets.

Again thanks for that!

you can use your browser console, so you can see the returned values from all function calls in the tests when you use “Run Tests”

1 Like

:open_mouth: didn’t knew that, neither have I tried though, I’ll test that out next time I have an issue.
Thanks!

Hello there.

Try it:

on my first attempt I got the result turned into a single array using .flat():

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

Then I replaced .flat() by using the ... rest operator into arr2 in .splice()

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

you are changing arr2, but the input array should stay the same after the function run, you have changed them