Update and rename drop-right.md to dropRight.md

This commit is contained in:
Angelos Chalaris
2017-12-19 12:06:47 +02:00
committed by GitHub
parent 911bf0a4dc
commit 340e58ddab
2 changed files with 12 additions and 13 deletions

12
snippets/dropRight.md Normal file
View File

@ -0,0 +1,12 @@
### Array dropRight
Returns a new array with `n` elements removed from the right
Check if `n` is shorter than the given array and use `Array.slice()` to slice it accordingly or return an empty array.
```js
const dropRight = (arr, n = 1) => n < arr.length ? arr.slice(0, arr.length - n) : []
//dropRight([1,2,3]) -> [1,2]
//dropRight([1,2,3], 2) -> [1]
//dropRight([1,2,3], 42) -> []
```