JS Built-ins
← Array75 / 226
ArrayArray.prototype.findIndexintermediate

Array.prototype.findIndex() β€” find string index by length

πŸ“– Usage Example
const words = ['apple', 'banana', 'cherry']
words.findIndex(w => w.startsWith('b'))  // β†’ 1

Use findIndex() to locate the position of a string that matches a pattern.

Array.prototype.findIndex() β€” strings

You can use findIndex() with strings just as easily as with numbers or objects.

Challenge: Implement indexOfLong(strs, len) that returns the index of the first string with length > len.

indexOfLong(['hi', 'hello', 'world'], 4) // β†’ 1
indexOfLong(['a', 'bb'], 5)              // β†’ -1
πŸ’‘ Hints (2)
  • β€’ `s.length > len` is strictly greater than β€” not `>=`.
  • β€’ The first qualifying index is returned, even if later strings are longer.