hey guys, I have completed this lesson but for the sake of the question I have altered my code.
Question is why anotherCopy splice 3,2,1 instead of 1,2,3 inside the normal loop. (to complete the lesson I had to use a backwards loop) ty.
**Your code so far**
function frankenSplice(arr1, arr2, n) {
let coppiedArray = [...arr1];
let anotherCopy = [...arr2];
for (let i = 0; i < coppiedArray.length; i++) {
let emptyArray = anotherCopy.splice(n, 0, coppiedArray[i])
}
//example let emptyArray = anotherCopy.splice(n,0,1,2,3);
console.log(anotherCopy)
return anotherCopy;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36
you mean I should use push() if I want to use a normal loop?
another question
I had some issues using splice, because it is also returning an empty array because of (n,0) so if you remove that assignment what happens to the empty array?
I didn’t say you should do anything, just explained why it’s that way.
Splice has an argument for where to start the insertion, so you could adjust that on each iteration.
So, if you wanted the result to looks like [4, 1, 2, 3, 4, 5, 6] then it could be
arr2Copy.splice(1, 0, 1); // insert 1 at index 1
arr2Copy.splice(2, 0, 2); // insert 2 at index 2
arr2Copy.splice(3, 0, 3); // insert 3 at index 3
I’ll leave it to you to figure out how to implement that in a loop.
Splice, unlike many other array prototype methods, mutates the array that it’s working on. If you don’t need the return value, you can just ignore it (in your code, you never used emptyArray).