Hey everyone! I’m a little confused on why my code doesn’t work, I checked the solution and I noticed they add each element of the first array individually into the second, what I did, was add the whole first array in the position specified, and I do get, in this case, [4, 1, 2, 3, 5], but it seems that it’s incorrect, any help here would be appreciated!
Your code so far
function frankenSplice(arr1, arr2, n) {
var arr2Copy=arr2.slice(0,arr2.length);
arr2Copy.splice(n,0,arr1);
return arr2Copy;
}
console.log(frankenSplice([1, 2, 3], [4, 5], 1));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
So, if you notice from the above example, we would need to do the following steps:
Make the copy of arr2. ( You’re doing fine there, but this would work too: var arr2Copy=arr2.slice(); Providing no arguments means to get a full copy of the array)
Extract the first n items from the copied array using splice. (The returned item will be a sub-array)
Concat them in order, extractedItems, arr1, copy2arr. cop2arr will be the remaining array after you’ve extracted the items.
If you reason about the above steps, they would make sense (given the nature of the current challenge)
If I can’t understand the challenge at first, I usually look what the return value has to be, and I start from there.