Slice and Splice: just a question

Slice and Splice: just a question
0

#1

Tell us what’s happening:

Hey!

This splice() is a very mysterious thing.

Everything is fine and my
code works, but I confused with one moment.
Why if I want to return a statement immediately it returns an empty array:

...
  return arrNew.splice(n, 0, ...arr1);
  //returns []
...

but when I first do the statement and then return a result it returns correct things:

...
  arrNew.splice(n, 0, ...arr1);
  return arrNew;
  //returns [4, 1, 2, 3, 5]
...

Thanks for your answers!

Your code so far


function frankenSplice(arr1, arr2, n) {
  let arrNew = [...arr2];
  arrNew.splice(n, 0, ...arr1);
  return arrNew;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

Take a look at my last reply to the thread below.

Also, whenever in doubt, you can consult the documentation to see what a method returns.


#3

Thank you @randelldawson for your answer!
I think I got! It’s the same if we create a new variable and assign there a statement. This case will return removed elements from array. But I just got that return is similar to a variable except that it stores nothing but returns immediately.