Slice and Splice Help please

Slice and Splice Help please
0

#1

Can someone please tell me why my code won’t work? Any help will be greatly appreciated!

function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!

arr2.splice(n, 0, arr1.slice(0, arr1.length));

return arr2;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);


#2

Check the return statement


#3

I tried the following and it doesn’t work either. :confused:

function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!
var newArray = [];

newArray = arr2.splice(n, 0, arr1.slice(0, arr1.length));

return newArray;
}


#4

“The input arrays should remain the same after the function runs.”

You’re changing the content of the second array using the splice method. While slice leaves the original array untouched, splice removes/adds certain elements from/to the array and modifies it.


#5

In addition to that you are also adding an entire array at position n instead of adding the elements of the array starting at position n. If you want to use splice, you could try using apply on Array.prototype.splice. It takes this and an array as arguments. It also only works for arrays with a max size of around 65k.


#6

Thanks for the help! Why doesn’t the following code work?

function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!

var arr1Copy = arr.slice();
var arr2Copy = arr.slice();

for (var i = 0; i < arr1.length; i++) {

arr2Copy.splice(n, 0, arr1Copy[i]);

}

return arr2Copy;

}

frankenSplice([1, 2, 3], [4, 5, 6], 1);


#7

Most issues with this challenge are related to last night’s release. I believe that this challenge was not supposed to be released.

http://forum.freecodecamp.com/t/slice-and-splice-errors-help-please/35497/5?u=matty22