Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Race Results Cleaner

Porting the Race Results Cleaner

In this lesson, you ported the timing desk’s results cleaner to JavaScript, up to the lookup of finish times, and ran both programs on the same export to check the report’s first line.

  • The Python script reads the chip-timing export, some rows with a stray space, a missing read, or a DNF: did not finish. It prints the race report: the bibs with a missing chip read, the average finish, and the fastest runner in each age group. The live results page is moving to JavaScript, so you rebuilt the script.
  • The port starts with an import: readFile() comes from the promise version of Node’s file system module. The folder’s package.json sets the type to module, so import works, and so does a top-level await.
  • to_seconds() becomes an arrow function with the same body, and Python’s map(int, ...) carries over as map(Number) here. Not map(parseInt): map() also passes each part’s index, and parseInt() reads that as a base.
  • to_record() trims every cell, and so does the port: its map() calls trim() on each one. The cells unpack into three names and a rest element, Python’s starred target. A rest element can only come last, so slice(0, -1) takes the checkpoints and at(-1) the finish.
  • The return builds the record with Number() for int() and shorthand for the rest: name alone means name: name. Mei Chen’s row comes out with its stray space gone and her missing read kept as an empty string.
  • Python’s with block opens the export, and its loop reads line by line. readFile() opens, reads and closes the JavaScript folder’s copy in one call, as one string, and await waits for it. Splitting the whole string leaves an empty last line, in Python too, so trim() comes first, and split() gives seven lines. map() turns each line into a record, and toRecord() ignores the index it also gets.
  • The finishers drop Lars Berg’s DNF and any row with no finish yet, as the live page runs from the start gun, before anyone finishes. &&, the JavaScript and, joins the two tests.
  • filter() keeps a finisher when not every checkpoint is there. every() does the work of all(), but needs a test: all() checks truthiness. Then map() picks out the bibs, after the filter, while each runner still has its checkpoints. Three rows have a gap, but missing holds only 23 and 58: the chain starts from the finishers, which left Lars Berg out.
  • Watch out: unlike Python’s empty list, an empty array counts as true, so if (missing) would print the label alone at the start gun. The port asks for the length instead. The spread then hands each bib to console.log() as its own argument, as the star does in Python’s call.
  • The dict comprehension becomes Object.fromEntries() over a map() of pairs, and the arrow’s parameter unpacks each record by name. The lookup’s keys are strings, as every object key is, where Python’s dict keeps numbers. A number bib still finds its entry, turned into the same string.

Here’s the desk’s Python script, results_cleaner.py:

Language: Python
def to_seconds(time):
    h, m, s = map(int, time.split(":"))
    return h * 3600 + m * 60 + s

def to_record(line):
    cells = [cell.strip() for cell in line.split(",")]
    bib, name, group, *splits = cells
    *checkpoints, finish = splits
    return {
        "bib": int(bib),
        "name": name,
        "group": group,
        "checkpoints": checkpoints,
        "finish": finish,
    }

with open("chip_reads.csv", encoding="utf-8") as f:
    runners = [to_record(line) for line in f]
finishers = [
    r for r in runners if r["finish"] not in ("", "DNF")
]

missing = [
    r["bib"] for r in finishers if not all(r["checkpoints"])
]
if missing:
    print("Missing chip reads:", *missing)

times = {r["bib"]: to_seconds(r["finish"]) for r in finishers}

average = sum(times.values()) / max(len(times), 1)
print(f"Average finish: {average / 60:.1f} min")

for group in sorted({r["group"] for r in runners}):
    fastest = min(
        (r for r in finishers if r["group"] == group),
        key=lambda r: times[r["bib"]],
        default=None,
    )
    if fastest is not None:
        print(f"{group}: {fastest['name']} {fastest['finish']}")

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this video, you’ll port the timing desk’s results cleaner to JavaScript. The Python script reads the chip-timing export, some rows with a stray space, a missing read, or a DNF: did not finish.

00:15 It prints the race report: the bibs with a missing chip read, the average finish, and the fastest runner in each age group. The live results page is moving to JavaScript, so you’ll rebuild the script until both programs print the report’s first line.

00:31 The average and the fastest runners are yours, in the next exercise.

00:37 The port starts with an import: readFile() comes from the promise version of Node’s file system module. The folder’s package.json sets the type to module, so import works here, and so does a top-level await.

00:52 to_seconds() becomes an arrow function with the same body. Python’s map(int, ...) carries over as map(Number) here.

01:00 Not map(parseInt): map() also passes each part’s index, and parseInt() reads that as a base.

01:10 to_record() trims every cell, and so does the port: its map() calls trim() on each one. The cells unpack into three names and a rest element, Python’s starred target.

01:23 A rest element can only come last, so the port takes the checkpoints with slice(), and the finish with at() from the end. The return builds the record with Number() for int(), and shorthand: name alone means name colon name.

01:39 Mei Chen’s row comes out with no stray space, and her missing read kept as an empty string.

01:46 Python’s with opens the export; its loop reads line by line. readFile() opens, reads and closes the JavaScript folder’s copy in one call, as one string, and await waits for it.

02:01 Splitting the whole string leaves an empty last line, in Python too, so trim() comes first. map() turns each line into a record; toRecord() ignores the extra index.

02:14 The finishers drop Lars Berg’s DNF, and any row with no finish yet, as the live page runs from the start gun. A double ampersand, the JavaScript and, joins the two tests.

02:29 filter() keeps a runner when not every checkpoint is there. every() does the work of all(), but needs a test: all() checks truthiness.

02:38 Then map() picks out the bibs, after the filter, while each runner still has its checkpoints. Three rows in the export have a gap. Which bibs end up in missing?

02:50 Your turn: pause the video and predict.

02:59 Only 23 and 58, Mei Chen and Sam Kowalski. Lars Berg has a gap too, but the chain starts from the finishers, which left him out. Python prints the line only when the list isn’t empty.

03:15 Watch out: unlike Python’s empty list, an empty array counts as true, so if (missing) would print the label alone at the start gun. The port asks for the length instead.

03:29 Inside, the spread hands each bib to console.log() as its own argument, as the star does in Python’s call.

03:38 Last, the dict comprehension becomes Object.fromEntries() over a map() of pairs. The arrow’s parameter unpacks each record by name, so the pair reads bib and finish directly.

03:52 The lookup’s keys are strings, as every object key is, where Python’s dict keeps numbers. A number bib still finds its entry, turned into the same string.

04:02 The Python script’s last lines read it for the average and fastest runners.

04:09 Both scripts run on the same export. Python prints its five lines. The port matches the first: missing chip reads, 23 and 58. The other four are still to port.

04:24 Comprehensions port to array methods, and unpacking to destructuring. Ask what you mean: an array’s length, not the array itself. Next up: the exercise Finish the Results Cleaner.

04:39 You’ll add the average finish line and, for each age group, the fastest finisher, so the JavaScript prints the Python’s whole report.

Become a Member to join the conversation.