In the last exercise, your checkRun() kept Ruth’s bad records out of the season log. It stopped each one with an error that named the run. Errors like those are the friendly kind of failure, because they tell you where to look. Some bugs don’t throw anything. The program runs to the end and prints a number that’s wrong.
In this lesson, you’ll track down a wrong result like that with a method instead of random edits. You’ll write down what you expected and what you got, make a guess that a check can prove wrong, and narrow the search until one record is left. Start with the number Priya Nair says is wrong.
Write Down What’s Wrong
A wrong result is only a bug once you can say exactly how it’s wrong, so the first step is to see it for yourself. Ruth has added the runs from April and May to runs.json, so the file now holds fourteen runs. If you’d like to follow along, re-create the files this lesson uses in your season-log folder:
This package.json tells Node.js to treat the .js files as ES modules:
package.json
{ "type": "module" }
The season in runs.json runs from April 4 to May 9:
runs.json
[
{ "id": 1, "runner": "Ruth Abebe", "date": "2026-04-04",
"distance": 5, "minutes": 27 },
{ "id": 2, "runner": "Priya Nair", "date": "2026-04-04",
"distance": 5, "minutes": 31 },
{ "id": 3, "runner": "Tomás Herrera", "date": "2026-04-04",
"distance": 8, "minutes": 44 },
{ "id": 4, "runner": "Grace Kim", "date": "2026-04-11",
"distance": 10, "minutes": 52 },
{ "id": 5, "runner": "Kwame Mensah", "date": "2026-04-11",
"distance": 5, "minutes": 25 },
{ "id": 6, "runner": "Ruth Abebe", "date": "2026-04-18",
"distance": 8, "minutes": 45 },
{ "id": 7, "runner": "Priya Nair ", "date": "2026-04-25",
"distance": 10, "minutes": 58 },
{ "id": 8, "runner": "Grace Kim", "date": "2026-04-25",
"distance": 5, "minutes": 26 },
{ "id": 9, "runner": "Tomás Herrera", "date": "2026-05-02",
"distance": 10, "minutes": 56 },
{ "id": 10, "runner": "Kwame Mensah", "date": "2026-05-02",
"distance": 8, "minutes": 38 },
{ "id": 11, "runner": "Ruth Abebe", "date": "2026-05-02",
"distance": 5, "minutes": 40 },
{ "id": 12, "runner": "Priya Nair", "date": "2026-05-09",
"distance": 5, "minutes": 33 },
{ "id": 13, "runner": "Kwame Mensah", "date": "2026-05-09",
"distance": 8, "minutes": 39 },
{ "id": 14, "runner": "Grace Kim", "date": "2026-05-09",
"distance": 5, "minutes": 25 }
]
The season.js module is the loader from the last lesson, which wraps a broken file’s SyntaxError:
season.js
import { readFileSync } from "node:fs";
export function loadSeason() {
const text = readFileSync("runs.json", "utf-8");
try {
return JSON.parse(text);
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error("Couldn't read runs.json", { cause: error });
}
throw error;
}
}
The club wants each member’s season totals so far. Ruth’s new module, club.js, groups the runs by runner. It keeps one totals object per name and adds each run to the object for its runner:
club.js
export function totalsByRunner(runs) {
const totals = {};
for (const run of runs) {
const name = run.runner;
if (totals[name] === undefined) {
totals[name] = { runs: 0, km: 0 };
}
totals[name].runs += 1;
totals[name].km += run.distance;
}
return totals;
}
A script, season-totals.js, prints one line for each of the club’s five members by looking their names up in the grouped totals:
season-totals.js
import { loadSeason } from "./season.js";
import { totalsByRunner } from "./club.js";
const members = [
"Ruth Abebe", "Priya Nair", "Tomás Herrera",
"Grace Kim", "Kwame Mensah",
];
const totals = totalsByRunner(loadSeason());
for (const name of members) {
const total = totals[name];
console.log(`${name}: ${total.runs} runs, ${total.km} km`);
}
The examples use Node.js 26. Run the script, and every member gets a line, with no error:
$ node season-totals.js
Ruth Abebe: 3 runs, 18 km
Priya Nair: 2 runs, 10 km
Tomás Herrera: 2 runs, 18 km
Grace Kim: 3 runs, 20 km
Kwame Mensah: 3 runs, 21 km
Ruth posts the list to the club, and Priya replies within the hour. Her own log says she ran three times: 5 km on April 4, 10 km on April 25, and 5 km on May 9. That adds up to 20 km, but the list shows 10.
There’s no error report this time, so there’s no top line to read and no at line pointing at your code. Instead, you start a debugging card, four lines that you fill in before you change any code. The first two say what’s wrong:
- Expected: Priya Nair, 3 runs, 20 km, from her own log
- Actual: Priya Nair, 2 runs, 10 km
- Hypothesis: A guess at the cause, specific enough that a check could prove it wrong
- Check: Something you run, with real values, whose result tells you whether the guess holds
Take the time to write the expected value down. It’s what tells you when the bug is gone. It also has to come from outside the program, such as Priya’s own log, because the program’s output can’t tell you whether that output is right.