Update takeWhile.md
This commit is contained in:
committed by
GitHub
parent
24f8b95f80
commit
c00348686e
@ -11,11 +11,11 @@ Returns the removed elements.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const takeWhile = (arr, func) => {
|
const takeWhile = (arr, func) => {
|
||||||
for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
|
for (const [i, val] of arr.entries()) if (!func(val)) return arr.slice(0, i);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]
|
takeWhile([1, 2, 3, 4], n => n < 3); // [1, 2]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user