just slice arr once

use variable `rightIndex` to make it more efficient, just slice arr once.
This commit is contained in:
Henry
2019-05-04 16:32:47 +08:00
committed by GitHub
parent 36fd8c91bd
commit 8440492710

View File

@ -7,8 +7,9 @@ Returns the remaining elements.
```js
const dropRightWhile = (arr, func) => {
while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
return arr;
let rightIndex = arr.length;
while (rightIndex-- && !func(arr[rightIndex]));
return arr.slice(0, rightIndex + 1);
};
```