Hi,
So, for the life of me I cannot understand why my output is returning arr1 in reverse order when spliced into my newly created arr2Copy, I’m sure it’s something really simple that I just cannot see, but god do I feel dumb right now trying to resolve the challenge.
My thought process is that I’ve created a loop, that confirms 0 is less than arr1 (which is 1 as it’s a single array), so it executes, gets the arr2Copy, and splices in from the user provided index (n), all the elements of arr1, hence arr1[I] within the loop, but it seems to be starting from the end of arr1 and not the start???
I think the problem might be with my thought process at the loop stage and what is actually happening within that part of the code block but I wanted to ask the community to shed a little light on this instead of looking at the answers.
The returned output is: [ 4, 3, 2, 1, 5, 6 ]
Hope the above makes sense.
Your code so far
function frankenSplice(arr1, arr2, n) {
let arr2Copy = arr2.slice(0, arr2.length); // this is a copy of arr2 as arr2 should not be mutated
for(let i = 0; i < arr1.length; i++){
arr2Copy.splice(n, 0, arr1[i]);
}
console.log(arr2Copy) // visualise the output
return arr2Copy;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.
Challenge: Slice and Splice
Link to the challenge: