Update takeWhile.md

This commit is contained in:
Chaitanya Chandurkar
2020-11-22 01:08:27 -05:00
committed by GitHub
parent 24f8b95f80
commit c00348686e

View File

@ -11,11 +11,11 @@ Returns the removed elements.
```js
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;
};
```
```js
takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]
takeWhile([1, 2, 3, 4], n => n < 3); // [1, 2]
```