djma777
September 23, 2019, 12:06pm
1
I wonder why my code wouldn’t pass the challenge. I print the result of my splice()
and seems to be giving the result I want. Input arrays were not changed, and I have the index n
just right. Hint me please. Tia!
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let newArr2 = arr2.slice()
newArr2.splice(n, 0, arr1);
console.log(newArr2)
return newArr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
djma777
September 23, 2019, 12:23pm
2
Figured it out. Here’s my new code:
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let newArr2 = arr2.slice();
for (let i = 0; i < arr1.length; i++){
newArr2.splice(n++, 0, arr1[i]);
}
return newArr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
So the problem was that my first code adds an array (arr1) inside arr2. I was trying not to use a for loop
, but had to anyways.