From 84404927105084baae5833482d0b143777bdd6d8 Mon Sep 17 00:00:00 2001 From: Henry <617822642@qq.com> Date: Sat, 4 May 2019 16:32:47 +0800 Subject: [PATCH] just slice arr once use variable `rightIndex` to make it more efficient, just slice arr once. --- snippets/dropRightWhile.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/dropRightWhile.md b/snippets/dropRightWhile.md index 48f496f51..4242132da 100644 --- a/snippets/dropRightWhile.md +++ b/snippets/dropRightWhile.md @@ -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); }; ```