Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
4.5. Find the first and last index matching a test
Easy
Topics
This question is in
4. Array utilitiesA 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
function findIndex(array, predicate, fromIndex) // returns a number
function findLastIndex(array, predicate, fromIndex) // returns a numberpredicate(value, index, array): returns a truthy value for the item you are looking for.fromIndex: where the search starts.findIndexstarts at0by default and moves forward;findLastIndexstarts at the last index and moves backward.- Both return the index of the item found, or
-1.
Requirements
findIndexreturns the index of the first matching item;findLastIndexthe index of the last one.- Both return
-1when no item matches, and for an empty array (without callingpredicate). - Both stop calling
predicateas soon as an item matches. predicategets the value, the index and the array. Any truthy result is a match.- Holes are checked too, as
undefined. findIndexstarts atfromIndex. A negativefromIndexcounts from the end (-2is the second-to-last index); one further back than the start means0; one at or past the end finds nothing.findLastIndexstarts atfromIndexand moves toward0. A negativefromIndexcounts from the end; one past the end means the last index; one further back than the start finds nothing.- A fractional
fromIndexis cut toward zero. Array.prototype.findIndexandArray.prototype.findLastIndexare not used.
Examples
const tasks = [{ done: true }, { done: false }, { done: true }, { done: false }];
findIndex(tasks, (task) => !task.done); // 1
findLastIndex(tasks, (task) => !task.done); // 3findIndex([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 backfindIndex([1, 2, 3], (n) => n > 9); // -1
findIndex([1, , 3], (n) => n === undefined); // 1: the holeEnvironment
- Plain JavaScript in a browser page; the tests call the globals
findIndexandfindLastIndexfromfindIndex.js. - The built-in
findIndexandfindLastIndexare removed while the tests run. Other built-ins may be used. fromIndexis a number or missing.
Hints
- Turn
fromIndexinto a real starting index before the loop. What are the smallest and largest useful values in each direction? - 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…