Merge pull request #201 from iamsoorena/update-arr-last-element
there is no need to make another array to return the last element. th…
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
### Last of list
|
### Last of list
|
||||||
|
|
||||||
Use `arr.slice(-1)[0]` to get the last element of the given array.
|
Use `arr.length - 1` to compute index of the last element of the given array and returning it.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const last = arr => arr.slice(-1)[0];
|
const last = arr => arr[arr.length - 1];
|
||||||
// last([1,2,3]) -> 3
|
// last([1,2,3]) -> 3
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user