Challenge Slice and Splice - help needed

Hi,

For me the return value seem to be ok. Have I missed something, please help :slight_smile:

Here is my code:

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

// Elements from index n of the second array to first item
res = arr2.slice(0, n);

// Add arr1 to res
res.splice(n, 0, arr1);

// Add rest of the arr2 to res
res.splice(res.length, 0, arr2.slice(n));

return res;
}

Hi

I’ve used a for loop to add the values of arr1 into arr2. My code below seems to give all the right values, but still not working!!

function frankenSplice(arr1, arr2, n) {
var result = arr2.slice(0);
for (var i=0; i<=arr1.length - 1; i++){
result.splice(n + i,0,arr1[i]);
console.log(arr2);
console.log(arr1)
}
return result;
}

Hello,

While mine is not the cleanest code, it seems to be working:

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

newArray.splice(n, 0, arr1);
newArray = [].concat.apply([], newArray);

console.log(newArray);
return newArray;
}

frankenSplice([“claw”, “tentacle”], [“head”, “shoulders”, “knees”, “toes”], 2);

Hello zumartic,

Since you are calling splice with the array itself, you will splice the array itself inside of the object, instead of the elements themselves which is what you need to do.

For example, calling [1, 2, 3].splice(3, 0, [4, 5, 6]) will give you [1, 2, 3, [4, 5, 6]] instead of [1, 2, 3, 4, 5, 6]. I would look into Function.prototype.apply to turn the array into arguments that are usable.

Also you could take a look at concat instead of splice for your particular algorithm, as it seems you are attempting to concatenate arrays together.

2 Likes

Hmm, I tried this one and I got [1, 2, 3, 4, 5, 6]. Below is quote from w3school and it tell the same thing. I wanted to use slice and splice as said in the challenge.

Add items to the array:

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];

fruits.splice(2, 0, “Lemon”, “Kiwi”);

The result of fruits will be:

Banana,Orange,Lemon,Kiwi,Apple,Mango

If you execute a = [1, 2, 3]; a.splice(3, 0, 4, 5, 6); a will be [1, 2, 3, 4, 5, 6].
If you execute a = [1, 2, 3]; a.splice(3, 0, [4, 5, 6]); a will be [1, 2, 3, [4, 5, 6]].
I would double check your code, as you may have forgotten the brackets in my example.

In the example I gave, there were brackets around 4, 5, 6, making it an array. If you put an array as the third argument for splice, then the array itself will be spliced ), not the elements of the array. In the example from w3schools, the call to splice has two additional arguments, “Lemon” and “Kiwi”, instead of one argument [“Lemon”, “Kiwi”]. If you want the internal elements of an array variable instead of the array itself to be inserted with splice, you need to transform them into individual arguments, such as with function.apply:

Thanks @eliotn I got it now :slight_smile:

-Zumi-

I know there are some solutions here, and since mine is similar to your original one, I thought I’d throw my code in.

function frankenSplice(arr1, arr2, n) {
  //slice to make a new array
  //splice to add it in
var splitA = arr2.slice(0,n);

var splitB = arr2.slice(n);

var midP = arr1;

return splitA.concat(midP,splitB);
}

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

I read an article that discussed some methods which led me to concat that fit nicely here. I had a less verbose code, but I had trouble with the subarray!

Hello,

I think this might help,

function frankenSplice(arr1, arr2, n) {
//create a new array and assign a duplicate of arr2
let newArr = arr2.slice();
//now add arr1 to new array using splice at location n
newArr.splice(n,0, …arr1);
return newArr;
}

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

Please let me know of possible improvements.

Thanks :slight_smile:

1 Like