Overview
Implement dropWhile(array, predicate) that drops elements from the start while predicate returns true.
Examples
dropWhile([1,2,3,4], x => x < 3); // [3,4]
Solution
Reveal solution
function dropWhile(array, predicate) {
let i = 0;
while (i < array.length && predicate(array[i], i, array)) i++;
return array.slice(i);
}drop-while.js
Drop While
easycodingJavaScriptArrays
Overview
Implement dropWhile(array, predicate) that drops elements from the start while predicate returns true.
Examples
dropWhile([1,2,3,4], x => x < 3); // [3,4]
Solution
Reveal solution
function dropWhile(array, predicate) {
let i = 0;
while (i < array.length && predicate(array[i], i, array)) i++;
return array.slice(i);
}NameTopicDifficulty
103 of 103 problems