Slice and Splice: Help understanding why setting my variable returns an empty array

Tell us what’s happening:
Hello, I solved the challenge, but along the way I ran into a problem where I ended up with an empty array after I had set my combinedArrs variable = to my spliced combinedArrs array.
I’m having trouble understanding why that resulted in an empty array. It must mean I am misunderstanding something and I don’t want to continue with flawed logic in my own reasoning.
The provided code is below with comments.
Appreciate the help :smiley:

  **Your code so far**

function frankenSplice(arr1, arr2, n) {
let combinedArrs = arr2.slice(0);
// currently working code which passes the challenge
combinedArrs.splice(n, 0, ...arr1);
// but if I use the following line instead, it returns an empty array instead
// combinedArrs = combinedArrs.splice(n, 0, ...arr1);
return combinedArrs;


console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
  **Your browser information:**

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

Challenge: Slice and Splice

Link to the challenge:

splice returns an array with the removed elements, if the second argument is 0 no element is remvoed and it returns an empty array

1 Like

but the 3rd argument in my splice method should add the elements of arr1 to arr2 at the index position n, shouldn’t it?
That way, it shouldn’t be empty.
It works as intended when I don’t add the extra "combinedArrs = ".

splice changes the array but also returns a new array with the removed elements

let arr = [0,1,2,3,8,4,5,8,9];

let splice1 = arr.splice(4,1);

console.log(splice1); // one element was removed, this is [8]
console.log(arr); // now without the removed element [0,1,2,3,4,5,8,9]

let splice2 = arr.splice(6,0,6,7);

console.log(splice2); // no elements were remove so this is []
console.log(arr) // now with the added elements [0,1,2,3,4,5,6,7,8,9]
1 Like

ahhh! I think I understand what you are saying now.
the value of the my splice is , because I didn’t remove any elements so it returns .
but when I USE the splice method on arr1, THAT is what was modified.
Thank you! It was bugging me why I couldn’t figure out where I misunderstood something.