Drop Right While
JS Functions
Easy
10 min
arrays
lodash
higher-order functions
Implement dropRightWhile(array, predicate). Starting from the end of array and moving backwards, it drops elements while predicate returns a truthy value. It returns a new array with everything up to and including the last element that failed the predicate.
dropRightWhile([1, 2, 3, 4], (n) => n > 2); // [1, 2]
dropRightWhile([5, 1, 6, 7], (n) => n > 2); // [5, 1] (5 is kept: the scan stopped at 1)
dropRightWhile([1, 2], (n) => n > 2); // [1, 2]
dropRightWhile([3, 4], (n) => n > 2); // []
// Trim trailing empty rows from a CSV
dropRightWhile([['a', 1], ['b', 2], [], []], (row) => row.length === 0); // [['a', 1], ['b', 2]]
Requirements
predicateis called aspredicate(value, index, array), whereindexis the element's real index inarray.- Elements are visited from the last one backwards. Stop calling
predicateas soon as it returns a falsy value. - Return a new array. Don't mutate the input.
Follow-up
- How would you implement this with the built-in
findLastIndex? - Write
takeRightWhile, which returns the dropped part instead.
Spotted a mistake or a rights concern? Report an issue