Skip to curriculum
Jump to the editor

4.5. Find the first and last index matching a test

Easy
Topics

This question is in

4. Array utilities
10–20 min

A to-do list needs the position of the first unfinished task to scroll to it. An editor that keeps an undo history needs the last entry saved before a given point. A table looks for the first row after the current one that matches a search. Each time, the answer is not the item but its index, and the search may start somewhere in the middle.

Write findIndex(array, predicate, fromIndex) and findLastIndex(array, predicate, fromIndex) in findIndex.js, without the built-in methods of the same names.

Signature

JAVASCRIPT
function findIndex(array, predicate, fromIndex)     // returns a number
function findLastIndex(array, predicate, fromIndex) // returns a number
  • predicate(value, index, array): returns a truthy value for the item you are looking for.
  • fromIndex: where the search starts. findIndex starts at 0 by default and moves forward; findLastIndex starts at the last index and moves backward.
  • Both return the index of the item found, or -1.

Requirements

  1. findIndex returns the index of the first matching item; findLastIndex the index of the last one.
  2. Both return -1 when no item matches, and for an empty array (without calling predicate).
  3. Both stop calling predicate as soon as an item matches.
  4. predicate gets the value, the index and the array. Any truthy result is a match.
  5. Holes are checked too, as undefined.
  6. findIndex starts at fromIndex. A negative fromIndex counts from the end (-2 is the second-to-last index); one further back than the start means 0; one at or past the end finds nothing.
  7. findLastIndex starts at fromIndex and moves toward 0. A negative fromIndex counts from the end; one past the end means the last index; one further back than the start finds nothing.
  8. A fractional fromIndex is cut toward zero.
  9. Array.prototype.findIndex and Array.prototype.findLastIndex are not used.

Examples

JAVASCRIPT
const tasks = [{ done: true }, { done: false }, { done: true }, { done: false }];
findIndex(tasks, (task) => !task.done);       // 1
findLastIndex(tasks, (task) => !task.done);   // 3
JAVASCRIPT
findIndex([5, 1, 5, 1], (n) => n === 5, 1);        // 2: starts at index 1
findLastIndex([5, 1, 5, 1], (n) => n === 5, -3);   // 0: starts at index 1, moves back
JAVASCRIPT
findIndex([1, 2, 3], (n) => n > 9);   // -1
findIndex([1, , 3], (n) => n === undefined);   // 1: the hole

Environment

  • Plain JavaScript in a browser page; the tests call the globals findIndex and findLastIndex from findIndex.js.
  • The built-in findIndex and findLastIndex are removed while the tests run. Other built-ins may be used.
  • fromIndex is a number or missing.

Hints

  1. Turn fromIndex into a real starting index before the loop. What are the smallest and largest useful values in each direction?
  2. Which kind of loop lets you leave as soon as you have the answer?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…