Skip to Main Content

Drop Right While

Problem URL:Drop Right While

My Solution

TypeScript

const dropRightWhile = <T>(
  array: Array<T>,
  predicate: (value: T, index: number, array: Array<T>) => boolean
): Array<T> => {
  let i = array.length - 1;

  while (predicate(array[i], i, array) && i >= 0) {
    i -= 1;
  }

  return array.slice(0, i + 1);
};

Let's Connect

Twitter GitHub LinkedIn