I created the solution below for the slice and splice javascript challenge. The commented out argument is the original idea that I had, but it would only give me empty arrays. Does anyone know how I could have made it all happen on one line like that?
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let arr=arr2.slice(0);
arr.splice(n,0,...arr1.slice(0))
//return arr2.slice(0).splice(n,0,arr1.slice(0));
return arr;
Not sure if you need to call slice(0)
after ...arr1
, since ...
points out to array elements.
Note about the commented line, splice
doesn’t return anything, so return arr.splice()
doesn’t make sense,
Also note when you splice another array into one array like the way commented like below
var n=1;
var a0=[1,2,3,4];
var a1=[5,6,7];
a0.splice(n,0,a1);
console.log(a0);//logs Array [1, Array [5, 6, 7], 2, 3, 4]
the whole array will be pushed to array as one element. but ...array
does the trick.
Also considering this thread