Hi there,
I have a couple of questions regarding the slice and splice exercise.
First question: I can’t understand why the use of the spread operator (…) would remove the brackets in the first array. My initial code , without the spread operator, would copy the items but would keep the [brackets]. Why is that? I see the use of the spread operator gives the expected answer. I’m just not sure why…
//initial code
copyOfArr2.splice(n,0, arr1); // the result here was [4, [1, 2, 3] 5, 6]
Second Question: I tried creating a variable that would hold the splice method for copyOfArr2, but when i tried returning that variable, it wouldn’t work. Why is that?
var newArr =copyOfArr2.splice(n,0, ...arr1);
**Your code so far**
The code below was accepted.
function frankenSplice(arr1, arr2, n) {
var copyOfArr2 = arr2.slice();
copyOfArr2.splice(n,0, ...arr1);
console.log(arr2);
console.log(copyOfArr2);
return copyOfArr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36
Challenge: Slice and Splice
Link to the challenge: