diff --git a/snippets/takeWhile.md b/snippets/takeWhile.md index 374606427..bc588936c 100644 --- a/snippets/takeWhile.md +++ b/snippets/takeWhile.md @@ -2,12 +2,12 @@ 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.keys()` until the returned value from the function is `true`. +Loop through the array, using a `for...of` loop over `Array.entries()` until the returned value from the function is `true`. Return the removed elements, using `Array.slice()`. ```js const takeWhile = (arr, func) => { - for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); + for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i); return arr; }; ``` diff --git a/test/takeWhile/takeWhile.js b/test/takeWhile/takeWhile.js index 1adb11cf0..79c3d432e 100644 --- a/test/takeWhile/takeWhile.js +++ b/test/takeWhile/takeWhile.js @@ -1,5 +1,5 @@ const takeWhile = (arr, func) => { -for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); +for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i); return arr; }; module.exports = takeWhile; \ No newline at end of file