Unsorted data: the silent wrong answer
No error and no warning anywhere: the wrong list looks exactly like the right one until you compare it with the truth.
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
sorted bisector says 2025-03-10 | truth is 2025-03-10
halves swapped bisector says 2025-06-30 | truth is 2025-03-10
shuffled bisector says 2025-01-19 | truth is 2025-03-10
sorted? true false false
wrong answers for the swapped list: 182 of 365
after sorting -> 2025-03-10