Finding the Fastest With Math.min()
The live results page names the fastest finish time and the runner who ran it. The Python script
finds both with min(): min(seconds) returns the smallest time, 5264 seconds, and
min(finishers, key=lambda r: to_seconds(r["finish"])) compares the finishers by their times and
returns the runner, Priya Nair.
- JavaScript keeps its minimum and maximum on the
Mathobject. Ported asMath.min(seconds), the line printsFastest finish: NaN, with no error and no warning. Math.min()compares the numbers you pass as separate arguments:Math.min(6157, 5448)is5448. It turns each argument into a number, and an array of six numbers isn’t one:Number(seconds)isNaN, and anyNaNargument makes the resultNaN. Python’smin()accepts one list or several values;Math.min()has only the second form.- Spread the array into the call,
Math.min(...seconds), like Python’s star inprint("Missing chip reads:", *missing), so each time becomes its own argument. It returns5264, as Python does, andMath.max(...seconds)returns6157, Ana’s time, the slowest finish. - At the start gun,
secondsis empty. Python’smin()raisesValueError: min() iterable argument is emptythere, unless you passdefault=.Math.min()with no numbers returnsInfinity, andMath.max()returns-Infinity, so check the array’s.lengthfirst. Math.min()has no key argument. For the runner, reduce the finishers to the best so far: the function takes the best runner and the next, and the conditional operator keeps the next runner only if their time is smaller.- With no start value,
reduce()takes the first runner as the best and calls the function five times for six runners: Tom replaces Ana, Priya replaces Tom, and Priya stays. Here the first runner is the right start, because the best so far has to be a real runner. - Like
min()with nodefault=, thisreduce()throws on an empty array (TypeError: Reduce of empty array with no initial value), so check the length first when that can happen. Like the desk’s Python, which has nodefault=yet, this port still stops at the start gun, after it logsInfinity. With<, a tie keeps the earlier runner, asmin()does. - When the numbers come from records,
map()them first, then spread that. A very long array, hundreds of thousands of numbers, can be too many arguments for one call, so reduce it instead.
Here’s the fastest finish and the fastest runner, with the records trimmed to the two fields this code reads, and the slowest finish logged as well:
const toSeconds = (time) => {
const [h, m, s] = time.split(":").map(Number);
return h * 3600 + m * 60 + s;
};
const runners = [
{ name: "Ana Ruiz", finish: "1:42:37" },
{ name: "Tom Okafor", finish: "1:30:48" },
{ name: "Mei Chen", finish: "1:36:20" },
{ name: "Lars Berg", finish: "DNF" },
{ name: "Priya Nair", finish: "1:27:44" },
{ name: "Sam Kowalski", finish: "1:35:03" },
{ name: "Jo Lindqvist", finish: "1:41:15" },
];
const finishers = runners.filter((r) => r.finish !== "DNF");
const seconds = finishers.map((r) => toSeconds(r.finish));
const fastest = Math.min(...seconds);
console.log("Fastest finish:", fastest);
console.log("Slowest finish:", Math.max(...seconds));
const winner = finishers.reduce((best, r) =>
toSeconds(r.finish) < toSeconds(best.finish) ? r : best
);
console.log("Fastest runner:", winner.name);