From 77dfa8831074dbab6e2edb4d0bd5d7e4007c91bc Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 1 Oct 2018 16:35:03 +0000 Subject: [PATCH] Travis build: 568 --- README.md | 10 +++------- docs/index.html | 7 ++----- snippets/takeRightWhile.md | 3 ++- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 276bdefcc..74cf86b39 100644 --- a/README.md +++ b/README.md @@ -2740,15 +2740,11 @@ takeRight([1, 2, 3]); // [3] 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.prototype.keys()` until the returned value from the function is `true`. -Return the removed elements, using `Array.prototype.reverse()` and `Array.prototype.slice()`. +Loop through the array, using a `Array.prototype.reduceRight()` and accumulating elements while the 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]), []); ```
diff --git a/docs/index.html b/docs/index.html index a7ea2b361..34e27fa4e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -513,11 +513,8 @@ console.log<

takeRight

Returns an array with n elements removed from the end.

Use Array.prototype.slice() to create a slice of the array with n elements taken from the end.

const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
 
takeRight([1, 2, 3], 2); // [ 2, 3 ]
 takeRight([1, 2, 3]); // [3]
-

takeRightWhile

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.prototype.keys() until the returned value from the function is true. Return the removed elements, using Array.prototype.reverse() and Array.prototype.slice().

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;
-};
+

takeRightWhile

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 accumulating elements while the function returns falsy value.

const takeRightWhile = (arr, func) =>
+  arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
 
takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
 

takeWhile

Removes elements in an array until the passed function returns true. Returns the removed elements.

Loop through the array, using a for...of loop over Array.prototype.entries() until the returned value from the function is true. Return the removed elements, using Array.prototype.slice().

const takeWhile = (arr, func) => {
   for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md
index 277ff969b..28a5c789a 100644
--- a/snippets/takeRightWhile.md
+++ b/snippets/takeRightWhile.md
@@ -5,7 +5,8 @@ Removes elements from the end of an array until the passed function returns `tru
 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], []);
+const takeRightWhile = (arr, func) =>
+  arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
 ```
 
 ```js