Your port can now turn the export’s text rows into runner records. Before the timing desk publishes anything, though, it asks questions about the whole field. Did anyone drop out? Which finishers are missing a chip read? And when a marshal calls in a bib number, who is it?
The Python script answers each question in one line, with any(), all(), and next(). In this lesson, you’ll port all three. JavaScript has the same answers, but it hangs them on the array instead of passing an iterable to a built-in function, and the last one doesn’t raise an error when nothing matches.
Ask Whether Anyone Dropped Out
The desk’s first question is whether anyone has a "DNF", because the results page then needs a “did not finish” section. To ask it, you’ll use the runners.js file in your race-results folder. This file holds the seven cleaned records, the same ones the Python script keeps in its runners and finishers lists. It exports runners in export order and finishers, the six without a "DNF":
runners.js
export const runners = [
{ bib: 7, name: "Ana Ruiz", group: "F35",
checkpoints: ["0:24:10", "0:48:35", "1:13:02"],
finish: "1:42:37" },
{ bib: 12, name: "Tom Okafor", group: "M40",
checkpoints: ["0:21:05", "0:42:30", "1:04:12"],
finish: "1:30:48" },
{ bib: 23, name: "Mei Chen", group: "F35",
checkpoints: ["0:22:40", "", "1:08:55"],
finish: "1:36:20" },
{ bib: 31, name: "Lars Berg", group: "M40",
checkpoints: ["0:23:15", "0:47:01", ""],
finish: "DNF" },
{ bib: 44, name: "Priya Nair", group: "F20",
checkpoints: ["0:20:30", "0:41:10", "1:02:05"],
finish: "1:27:44" },
{ bib: 58, name: "Sam Kowalski", group: "M40",
checkpoints: ["0:22:50", "0:45:12", ""],
finish: "1:35:03" },
{ bib: 66, name: "Jo Lindqvist", group: "F20",
checkpoints: ["0:23:30", "0:47:55", "1:12:40"],
finish: "1:41:15" },
];
export const finishers = runners.filter(
(r) => r.finish !== "DNF",
);
Each example imports the names it needs from that file, as Python’s from runners import finishers would. In Python, the DNF question is any() over a generator expression:
any(r["finish"] == "DNF" for r in runners)
That gives True, thanks to Lars Berg. JavaScript has no any() function. Arrays have a .some() method instead:
import { runners } from "./runners.js";
console.log(runners.some((r) => r.finish === "DNF"));
It gives the same Boolean answer:
true