JS Built-ins
← Array84 / 226
ArrayArray.prototype.findLastIndexbeginner

Array.prototype.findLastIndex() β€” returns -1 when not found

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.findLastIndex(n => n > 10)  // β†’ -1  (not found)

findLastIndex() returns -1 when no element satisfies the predicate.

Array.prototype.findLastIndex() β€” no match

Like all index-returning methods, findLastIndex() returns -1 when no element satisfies the predicate.

Challenge: Implement lastNegativeIndex(nums) that returns the index of the last negative number, or -1 if none.

lastNegativeIndex([1, -2, 3, -4]) // β†’ 3
lastNegativeIndex([1, 2, 3])      // β†’ -1
πŸ’‘ Hints (2)
  • β€’ Check the result against `-1` to determine if a match was found.
  • β€’ `n < 0` is false for zero β€” use `n <= 0` to include zero.