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