Basic Algorithm Scripting: Slice and Splice Easy

Tell us what’s happening:
I have a question,
Why I can’t return my result directly, like this.

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

Your code so far


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

frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

Because then you would be returning the return value from splice. You are assumin that that would be the resultant array, but it’s not - it’s an array of the deleted elements. Some methods return the result, some return something else. Some mutate the original (like this does) and some create a new array/object. I have to keep referring back to the docs.

1 Like