Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.3 — Finding the Nearest Point
Data Visualization · Example 2 of 3

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.

01

Write nearestDay(rows, date, maxDays) that returns the nearest row or null, and handles an empty array.

02

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

01

Why does bisector.center give the day under the pointer, but bisector.left can crash on the right edge?

02

What happens if you call a bisector on unsorted data, and why is that worse than an error?

03

Why build the Delaunay index in pixel space?

Unsorted data: the silent wrong answer

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