Since you are calling splice with the array itself, you will splice the array itself inside of the object, instead of the elements themselves which is what you need to do.
For example, calling [1, 2, 3].splice(3, 0, [4, 5, 6]) will give you [1, 2, 3, [4, 5, 6]] instead of [1, 2, 3, 4, 5, 6]. I would look into Function.prototype.apply to turn the array into arguments that are usable.
Also you could take a look at concat instead of splice for your particular algorithm, as it seems you are attempting to concatenate arrays together.
Hmm, I tried this one and I got [1, 2, 3, 4, 5, 6]. Below is quote from w3school and it tell the same thing. I wanted to use slice and splice as said in the challenge.
Add items to the array:
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
If you execute a = [1, 2, 3]; a.splice(3, 0, 4, 5, 6); a will be [1, 2, 3, 4, 5, 6].
If you execute a = [1, 2, 3]; a.splice(3, 0, [4, 5, 6]); a will be [1, 2, 3, [4, 5, 6]].
I would double check your code, as you may have forgotten the brackets in my example.
In the example I gave, there were brackets around 4, 5, 6, making it an array. If you put an array as the third argument for splice, then the array itself will be spliced ), not the elements of the array. In the example from w3schools, the call to splice has two additional arguments, “Lemon” and “Kiwi”, instead of one argument [“Lemon”, “Kiwi”]. If you want the internal elements of an array variable instead of the array itself to be inserted with splice, you need to transform them into individual arguments, such as with function.apply:
I know there are some solutions here, and since mine is similar to your original one, I thought I’d throw my code in.
function frankenSplice(arr1, arr2, n) {
//slice to make a new array
//splice to add it in
var splitA = arr2.slice(0,n);
var splitB = arr2.slice(n);
var midP = arr1;
return splitA.concat(midP,splitB);
}
frankenSplice([1, 2, 3], [4, 5], 1);
I read an article that discussed some methods which led me to concat that fit nicely here. I had a less verbose code, but I had trouble with the subarray!
function frankenSplice(arr1, arr2, n) {
//create a new array and assign a duplicate of arr2
let newArr = arr2.slice();
//now add arr1 to new array using splice at location n
newArr.splice(n,0, …arr1);
return newArr;
}