Slice and Splice empty array

I do not understand, why array “fr” return empty array?


function frankenSplice(arr1, arr2, n) {
  var a = arr1;
  var b = arr2;
var fr =[];
  
  return fr = b.splice(n, 0, a);
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 YaBrowser/19.9.0.1343 Yowser/2.5 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

splice changes the original array so if return b instead of fr you will get your answer

 b.splice(n, 0, a)
   return b

also you could use a spread operator on a

Anyway, gives an error…

function frankenSplice(arr1, arr2, n) {
  var a = arr1;
  var b = arr2;

 b.splice(n, 0, ...a);
 return b;
}

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

compilator si writing: ‘The second array should remain the same after the function runs.’

yes because arr2 must stay original so this is where slice comes in, use slice() to copy the array to another first hope this helps as i dont wanna just write the solution as thats not gonna help you

It worked. Thank you very much for your help.

1 Like