Slice and Splice can't understand why its failing


There is my code for Slice and Splice test:

function frankenSplice(arr1, arr2, n) {

  let newarr = arr2.slice();

  let ourarr = newarr.splice(n, 0, ...arr1);

  return ourarr;


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

Maybe someone could answer, why test fails, when I returning variable ourarr, but if I return variable newarr, test succeedes?

Thank You!

Hi @deividasn93

The reason its because you cannot assign newarr into ourrarr variable bacause the right hand side will evealute first and assign the value and return of that splice.

if you want to assing newarr into a variable ourrarr you will have to do this.

  let newarr = arr2.slice();
  newarr.splice(n, 0, ...arr1); 
  let ourarr = newarr;  // Here you are assigning your result splice into a variable. 
  return ourarr;

That method will work!
Hope it helps :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.