# Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
So I actually Passed all the tests with this code, but I was wondering if anyone had a better way to do this? It seems its a little overboard for what its doing. Any help would be awesome.

``````function frankenSplice(arr1, arr2, n) {
let newArr = [];
let newSpace = n;
let newSpace2 = n;
let arraySlice = arr1.slice()
let arraySlice2 = arr2.slice()
for (let i = 0; i < arraySlice2.length; i++){
let spliced = newArr.splice(newSpace, 0, addtoArray)
newSpace++;
console.log(newArr)
}
for (let i = 0; i < arraySlice.length; i++){
let spliced = newArr.splice(newSpace2, 0, addtoArray)
newSpace2++;
}
return newArr;
return arr2;
}

console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Slice and Splice

I would agree with this. Even though your code isn’t that complex, it still takes a lot of effort to figure out what it is doing since you’ve got two `for` loops and a lot of temporary variables to keep track of.

This challenge can be solved in just a few lines of code, or even one `return` statement if you really wanted to push your limits. Remember what you are doing here. You are inserting one array into another array (at a given index). Doesn’t this sound like something the `splice` method was meant to do? Why would you need all of those `for` loops when `splice` can do what you want all at once?

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