JS Built-ins
StringString.prototype.lastIndexOfintermediate

String.prototype.lastIndexOf() β€” fromIndex limits search

πŸ“– Usage Example
const str = 'abcabc'
str.lastIndexOf('a')    // β†’ 3
str.lastIndexOf('z')    // β†’ -1
str.lastIndexOf('b', 3) // β†’ 1

Use lastIndexOf() to find the index of the last occurrence of a substring, searching from right to left.

String.prototype.lastIndexOf() β€” fromIndex

The optional second argument fromIndex tells the search to start from that position and go backward.

Challenge: Implement lastBefore(str, sub, pos) using .lastIndexOf(sub, pos).

lastBefore('abcabc', 'a', 4) // β†’ 3
lastBefore('abcabc', 'a', 2) // β†’ 0
πŸ’‘ Hints (1)
  • β€’ `str.lastIndexOf(sub, fromIndex)` searches backward starting from `fromIndex`.