function slasher(arr, howMany) {
var output;
output = arr.slice(howMany, arr.length);
return output;
}
slasher([1, 2, 3], 2);
The way slice works is you give it a start point and an end point slice.(a, b)
and it returns you that portion, and gets rid of the rest so to speak.
so for [1, 2, 3]
, to get just the 3, I know it starts 2 (because 0, 1, 2), that point is also going to be the same as the howMany
input. So I used that. I also want to slice after that point, so it’d be 3, or the length of arr, arr.length
which is 3. SO in short, you want the point the element starts and the point after.
Sorry, this is probably poorly explained.
Another eg. if you had an array, array = [1, 2, 3, 4, 5]
and you wanted to slice out 3 and 4, you’d go array.slice(2, 4)
because [1, 2, (point 2) 3, 4,(point 4) 5]
if I just did arr.slice(2, 3)
, I’d only get 3 because[1, 2, (point 2) 3, (point 3) 4, 5]