Implement a function findLastIndex(array, predicate, [fromIndex=array.length-1]) that takes an array of values, a function predicate, and an optional fromIndex number argument. findLastIndex iterates over elements of the array from right to left.

The function returns the index of the first element that satisfies the predicate when iterating backwards from fromIndex.

Arguments

  1. array (Array): The array to inspect.
  2. predicate (Function): The function invoked per iteration.
  3. [fromIndex=array.length-1] (number): The index to start searching from (towards the left).

Predicate signature: The predicate function is invoked with three arguments: (value, index, array).

Returns

(number): Returns the index of the found element, else -1.

Examples

const arr = [5, 4, 3, 2, 1]; // Indices: 0, 1, 2, 3, 4

// Search for the element > 3, starting from the end (index 4).
findLastIndex(arr, (num) => num > 3); // => 1

// Start searching backwards from index 3.
// Check index 3: 2 > 1 is true. Stop.
findLastIndex(arr, (num) => num > 1, 3); // => 3

// Start searching backwards from index 3.
// Check index 3: 2 < 1 is false.
// Check index 2: 3 < 1 is false.
// Check index 1: 4 < 1 is false.
// Check index 0: 5 < 1 is false. Reached start and no element found.
findLastIndex(arr, (num) => num < 1, 3); // => -1

Edge cases

Your function should handle negative and out-of-bound fromIndex values.