Slice and Splice - getting an undefined value

Hello. I have no clue why the console is showing undefined after the last number from the first array.

function frankenSplice(arr1, arr2, n) {
  for(let i = arr1.length; i > -1; i--) {
    arr2.splice(n, 0, arr1[i])
  };
  console.log(arr2)
  return arr2;
};
frankenSplice([1, 2, 3], [4, 5, 6], 1);

Hi @madscience1,

Your for loop starts at index : arr1.length, which is equal to 3.
And, arr1[3] is undefined, that is why it’s returning undefined

You can fix that by starting your for loop with :

i = arr1.length -1

I hope this helps.

Cool, go it. Cheers @spark07

1 Like

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