Bisector: ties, edges, gaps and empty data
A pointer is never exactly on a data point. A bisector finds the nearest day on sorted data in a few steps, a Delaunay triangulation finds the nearest dot of a scatter plot, and unsorted data gives a wrong answer with no error.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 4 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write nearestDay(rows, date, maxDays) that returns the nearest row or null, and handles an empty array.
Write isSortedBy(rows, accessor) and use it to throw a clear error Rows must be sorted by date before building a chart.
Explain it without notes
Why does bisector.center give the day under the pointer, but bisector.left can crash on the right edge?
What happens if you call a bisector on unsorted data, and why is that worse than an error?
Why build the Delaunay index in pixel space?
Console
(nothing was printed)Expected console output
rows: 59 | first: 2025-01-01 | last: 2025-02-28
2025-01-05T11:00Z | left -> 2025-01-06 | center -> 2025-01-05
2025-01-05T13:00Z | left -> 2025-01-06 | center -> 2025-01-06
2025-01-05T12:00Z | left -> 2025-01-06 | center -> 2025-01-06
before the first day: left -> 0 | center -> 0
after the last day: left -> 59 (rows.length is 59) | center -> 58
15 Jan has no row, nearest is 2025-01-20
with a limit of 1.5 days: no data here | 2025-01-08
empty array: left 0 center 0 -> rows[0] is undefined