Slasher Flick confusion with splice function

Slasher Flick confusion with splice function
0.0 0

#1

Tell us what’s happening:
This doesnt make sense to me. according to everywhere else arr.splice(2) should return [1,2] (if youre using the first example) however it returns [3]. i thought the proper syntax was supposed to be arr.splice(0, howMany)… whats going on here?

Your code so far

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  arr = arr.splice(howMany);
  return arr;
}

slasher([1, 2, 3], 2);

Your browser information:

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

Link to the challenge:


#2
var arr = [1, 2, 3]
arr.splice(2) 

This will return 3 because splice(startIndex) return value is an array of the removed value.
So when you call splice(2) on the array, index 2 value which is 3 will return and the reason the function slasher is returning 3 is it’s saving arr.splice(2) return value which is 3.

If the function should return first 2 items from the array, it should be

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  arr.splice(howMany);
  return arr;
}

slasher([1, 2, 3], 2);

and this will return an array after removing the second item of the array which is [1, 2]

I’m not sure if I explain this enough let me know if you’re still confused! It took me so long to understand splice function :sweat_smile:

Add: I just checked the challenge, and to pass it your code is correct!