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); }; ```