# Slice and Splice problem

Slice and Splice problem
0

#1

Tell us what’s happening:
Please help me to correct this code as it give correct,but not on FCC

Your code so far

``````
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
var newArr=arr2.slice(0);
return newArr.splice(n,0,arr1);
}

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

Your browser information:

User Agent is: `Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36`.

#2

You have two separate issues going on with your current code.

1. splice returns an array with any elements which were removed. In your case, you specified 0 for the 2nd argument which means remove no elements, so an empty array is returned. To resolve, just implement the splice on one line and then return newArr on a separate line.

2. When you put arr1 as the 3rd argument of the splice function it actually puts the entire array as one single element and not the individual elements of the array arr1. After the splice is performed, newArr becomes [ 4, [ 1, 2, 3 ], 5, 6 ] instead of [4, 1, 2, 3, 5, 6] which is the correct return value.

You need to “spread” arr1 out. Review the previous ES6 challenges for how to use the spread syntax to do that.

#3

The only way I could get it to pass was to use this:

``````  let arrCopy = arr2.slice(0, arr2.length);
arrCopy.splice(n, 0, ...arr1);

return arrCopy;
``````

#4
``````function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
var newArr=[];
newArr=arr2.slice(0);
newArr.splice(n,0,...arr1);
return newArr;
}

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

this also works…

#5

thank u @RandellDawson
code works after applying first suggestion

#6

why cannot be splice assigned to variable?

``````var arr2copy = arr2.slice(0, arr2.length);

var out = arr2copy.splice(n,0,...arr1);

return out;
``````

#7

@lubodrinka
you can assign the return value of the splice into variable.But here in your code the 2nd arugment specify no element is to be remove from “arr2” and splice return empty array/no value.
hope you understand

#8

No.
it’s petervann solution and its working.
first create the shallow copy,
but there is still arr2copy to be assigned to out variable,
who create that logic must be such an idiot.

#9

It appears @NiteshTyagi has already explained why your solution above will not return the correct answer. The splice method returns an array of all the elements removed, so since 2nd argument’s value is 0, nothing gets removed. However, since the method still returns an array, it just is an empty array.

FYI - For the shallow copy, the slice can simply be written as:

``````arr2.slice();
``````

#10

that is different explanation. he wrote so, you can explain in two ways.

#11

I was trying to solve the problem almost exactly the same way you did,
but I was doing […arr1] instead of simply …arr1.
I though the spread operator must be in brackets.
Anyway thanks for the answer.
By the way here’s how I finally solved it.
I found a way to flatten the array

spoiler
``````function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let arr3 = [...arr2];
let arr4 = [];

arr3.splice(n, 0, arr1.slice(0))
arr4 = [].concat.apply([], arr3)
console.log(arr3);

return arr4;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
``````