[Easy] dropRightWhile

January 28, 2024

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

Question Prompt

Let's consider a function called dropRightWhile. Its purpose is to create a new slice of an array, excluding certain elements from the end. The elements to be excluded are determined based on a condition specified by a predicate function. It's crucial that the original array remains intact.

Here's the specific task:

  1. Implement the dropRightWhile(array, predicate) function.

  2. Ensure that the function adheres to the following requirements:

  • It accepts two arguments:
    • array: The array to be processed.
    • predicate: A function that determines which elements to drop.
  • It creates a new array, not modifying the original array.
  • It iterates through the array from the end, dropping elements as long as the predicate function returns a truthy value.
  • It stops dropping elements as soon as the predicate returns a falsy value.
  • It returns the final slice of the array, containing the remaining elements.
dropRightWhile(
  ["hello", "world", "today", "isGood"],
  (value) => value.length > 5
); // =>  ['hello', 'world', 'today']
dropRightWhile(
  [
    { name: "Alice", age: 25 },
    { name: "Charlie", age: 20 },
    { name: "Bob", age: 30 },
  ],
  (obj) => obj.age > 25
); // => [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 20 }]
dropRightWhile([10, 20, 30, 40, 50, 10], (value) => value !== 10); // => [10, 20, 30, 40, 50, 10]
dropRightWhile([1], (value) => value > 0); // => []

Solutions

  • Begins by positioning a pointer called index at the very end of the array, ready to scan elements from right to left.
  • Enters a loop that continues as long as:
    • There are still elements to check (index >= 0).
    • The predicate function (predicate(array[index], index, array)) returns true, indicating the current element should be dropped.
  • Inside the loop, drops elements by simply moving the index pointer one step to the left by index--.
  • Once the loop stops (either because the array is fully processed or the predicate returns false), it means the remaining elements on the left should be kept.
  • Then, uses array.slice(0, index + 1) to create a new array containing only those elements from the beginning up to (and including) the last element that didn't meet the predicate's condition. This new slice of the original array is returned as the final result.
function dropRightWhile(array, predicate) {
  let index = array.length - 1;

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

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

If the interviewer ask you to write it with for loop. You can do the following instead.

  • Start by looping through the array in reverse order, starting from the last element. This is done using a for loop that counts down from array.length - 1 to 0.
  • For each element, it calls the predicate function, passing the element, its index, and the entire array as arguments. If the predicate returns false, it means the current element should be kept.
  • As soon as the predicate returns false, the loop stops, and it creates a new array containing only the elements up to (and including) the current element. This slicing is done using array.slice(0, i + 1). This new array is then returned, containing the elements that should be kept.
  • If the loop completes without ever hitting a false from the predicate, it means all elements meet the condition for removal. In this case, an empty array ([]) is returned.
function dropRightWhile(array, predicate) {
  for (let i = array.length - 1; i >= 0; i--) {
    if (!predicate(array[i], i, array)) {
      return array.slice(0, i + 1); // Return as soon as predicate is false
    }
  }
  return []; // If all elements meet the predicate, return an empty array
}
☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee