Totaling With reduce()
The line that prints the average finish time has to work at the start gun, when the results page
goes up and nobody has finished. The Python script adds the finish times with sum() and divides by
max(len(seconds), 1), and prints Average finish: 95.6 min for the six finishers and
Average finish: 0.0 min at the start gun, where nobody has a finish yet.
- JavaScript has no
sum()built-in. Arrays have areduce()method, which folds the whole array into one value with a function you pass it, as with Python’sfunctools.reduce().Math.max()keeps the guard, and.toFixed(1)rounds as:.1fdoes. - Ported as
seconds.reduce((a, b) => a + b), the average matches for the race export. At the start gun,secondsis empty, and the port prints no average: it stops withTypeError: Reduce of empty array with no initial value. - Without an initial value,
reduce()takes the array’s first item as the total and calls the function from the second item on. An empty array has no first item, so there’s no total to start from, andreduce()throws. - Python’s
sum()has its start built in, astartparameter that defaults to0, so the sum of an empty list is0. Python’sfunctools.reduce()works like JavaScript’sreduce(): given an empty list and no initial value, it raisesTypeError: reduce() of empty iterable with no initial value. - Pass the starting value as the second argument, and name the parameters for what they hold:
seconds.reduce((total, s) => total + s, 0). The total starts at0, and each call adds one finish time and returns the new total, which goes into the next call:6157,11605,17385,22649,28352and34427seconds. On an empty array,reduce()never calls the function and returns0. - To count, as
sum(1 for r in runners if r["finish"] == "DNF")does, keep the matches withfilter()and read.length, aslen()of a list comprehension would: one runner didn’t finish, and at the start gun, nobody. - A
reduce()is a loop with a running total. When its function would take more than one line to follow, write that loop withfor...of, alettotal that starts at0, and+=. - A newer built-in,
Math.sumPrecise(), adds floats at least as carefully as Python’ssum(), but it isn’t in Node 26.
Here’s the average and the count, with the records trimmed to the two fields this code reads:
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 sum = seconds.reduce((total, s) => total + s, 0);
const average = sum / Math.max(seconds.length, 1);
console.log(`Average finish: ${(average / 60).toFixed(1)} min`);
const dnf = runners.filter((r) => r.finish === "DNF").length;
console.log("Did not finish:", dnf);