From 9c6087c4acb1f2c5fdb2c8f23f4057c17a3c2e24 Mon Sep 17 00:00:00 2001 From: Siarhei Date: Thu, 20 Sep 2018 18:10:31 +0400 Subject: [PATCH 1/3] Refactor takeRightWhile --- snippets/takeRightWhile.md | 9 ++------- test/takeRightWhile/takeRightWhile.js | 6 +----- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md index 526124ac8..670dfbd67 100644 --- a/snippets/takeRightWhile.md +++ b/snippets/takeRightWhile.md @@ -2,15 +2,10 @@ Removes elements from the end of an array until the passed function returns `true`. Returns the removed elements. -Loop through the array, using a `for...of` loop over `Array.keys()` until the returned value from the function is `true`. -Return the removed elements, using `Array.reverse()` and `Array.slice()`. +Loop through the array, using a `Array.prototype.reduceRight` and accumulate element if function returns falsy value. ```js -const takeRightWhile = (arr, func) => { - for (let i of arr.reverse().keys()) - if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length); - return arr; -}; +const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []); ``` ```js diff --git a/test/takeRightWhile/takeRightWhile.js b/test/takeRightWhile/takeRightWhile.js index 67cbe63b1..29011dc3d 100644 --- a/test/takeRightWhile/takeRightWhile.js +++ b/test/takeRightWhile/takeRightWhile.js @@ -1,6 +1,2 @@ -const takeRightWhile = (arr, func) => { - for (let i of arr.reverse().keys()) - if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length); - return arr; -}; +const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []); module.exports = takeRightWhile; From 95262b249dbfdcaabf00e272ff2433caa2e867c1 Mon Sep 17 00:00:00 2001 From: Siarhei Date: Tue, 25 Sep 2018 10:13:32 +0400 Subject: [PATCH 2/3] Fix takeRightWhile doc --- snippets/takeRightWhile.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md index 670dfbd67..e62b63940 100644 --- a/snippets/takeRightWhile.md +++ b/snippets/takeRightWhile.md @@ -2,7 +2,7 @@ Removes elements from the end of an array until the passed function returns `true`. Returns the removed elements. -Loop through the array, using a `Array.prototype.reduceRight` and accumulate element if function returns falsy value. +Loop through the array, using a `Array.reduceRight` and accumulate element if function returns falsy value. ```js const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []); From d8a1c09133151f6056fba906323a174657d923c0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Oct 2018 19:32:54 +0300 Subject: [PATCH 3/3] Update takeRightWhile.md --- snippets/takeRightWhile.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md index e62b63940..277ff969b 100644 --- a/snippets/takeRightWhile.md +++ b/snippets/takeRightWhile.md @@ -2,7 +2,7 @@ Removes elements from the end of an array until the passed function returns `true`. Returns the removed elements. -Loop through the array, using a `Array.reduceRight` and accumulate element if function returns falsy value. +Loop through the array, using a `Array.prototype.reduceRight()` and accumulating elements while the function returns falsy value. ```js const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []);