Exercise: Training Log
Build the running club's training-log command, which reads a CSV file, prints one runner's week, saves it as JSON when asked, takes its data folder from an environment variable, and reports a missing file or an unknown runner with a clear message and exit code 1, while tests run it as its own program.
Apprentice · Node.js · JavaScript · Updated
Every Sunday night, Dana, the Trailhead Running Club's coach, adds up each runner's week by hand. You'll build the command that does it for her: node log.js --runner Maya --week 2026-11-02 reads the club's log, prints Maya's week (her runs, her total miles, her average pace and her longest run), saves it as JSON when asked, and fails with a clear message and the right exit code when something's wrong. Everything this chapter taught goes into it, and tests run it the way Dana will: as a command.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-node-training-log, also linked from the Exercise box beside the lesson. Clone it into your nodejs-course folder. In VS Code's terminal:
cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-training-log.git
Git makes a folder, exercise-node-training-log, with the exercise code inside. Open that folder in VS Code (File > Open Folder), and open its terminal with Terminal > New Terminal. There's nothing to install: the tests use Node's own test runner. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.
The folder holds:
training.js: the calculations,parseRuns,runsForWeekandsummarize, for you to finish.log.js: the command itself, for you to finish.helpers.js:addDays, which gives the date some days later, andformatPace, which writes a pace such as9:35. Ready to use.data/runs.csv: the club's log, 17 runs from October 26 to November 14, 2026.log.test.js: the tests. You don't need to change it.
What to do
The comment at the top of training.js says what each function returns, with an example. The comment at the top of log.js lists every command it should answer. Here's the heart of it:
// node log.js --runner Maya --week 2026-11-02
// prints these five lines, and the exit code is 0:
// Maya's week of 2026-11-02
// Runs: 3
// Total: 14.5 miles
// Average pace: 9:35 per mile
// Longest run: 6.2 miles
//
// node log.js --runner Maya --week 2026-11-02 --out summary.json
// prints the same five lines, writes the summary to summary.json as JSON with two-space indents,
// then prints one more line: Saved the summary to summary.json
//
// The errors: each message goes to console.error, nothing else is printed, and the exit code is 1.
// No --runner, or no --week:
// Give a runner and a week, such as: node log.js --runner Maya --week 2026-11-02
// No runs.csv in the folder:
// Can't find the training log at <the full path of runs.csv>. Set RUNLOG_DIR to the folder that holds runs.csv.
// A runner with no runs anywhere in the log, such as Zoe:
// There's no runner named Zoe in the training log.
log.js's comment also covers --help, the short flags (-r, -w, -o, -h) and a runner with no runs that week.
- Turn the log's text into runs. Finish
parseRunsintraining.js: split the text into lines, skip the header and the blank lines, and turn each line into an object with its miles and minutes as numbers. - Pick out one runner's week. Finish
runsForWeek: that runner's runs from the Monday given to the Sunday six days later, both included. - Sum up the week. Finish
summarize: how many runs, the total miles, the average pace and the longest run. - Read the options. In
log.js, read--runner,--week,--outand--helpwithparseArgs, and answer--helpand a missing runner or week. - Find and read the log. Read
runs.csvfrom the folderRUNLOG_DIRnames, or, when it isn't set, from thedatafolder besidelog.js. Report a missing file with its full path. - Print the week, and save it when asked. Check that the runner is in the log, print the five lines, and write the JSON file when
--outis given.
Check your work
Run the tests from the exercise folder:
npm test
At first all 16 tests fail. That's expected: tests 1 to 6 turn green as you finish training.js, and tests 7 to 16, which run log.js as its own program, as you finish the command. A failing test's name says what it ran and what it expected, such as 11) With no --week, it prints "Give a runner and a week ..." to the error output and exits with 1.
To try the command yourself, run it in the terminal, then check its exit code with $LASTEXITCODE in PowerShell, or echo $? in zsh:
node log.js --runner Maya --week 2026-11-02
Hints
- Finish
training.jsfirst.log.jsneeds all three functions, and tests 1 to 6 check each one on its own. From Text to Data: Lines, CSV and JSON Files splits the same kind of log. - Dates written as yyyy-mm-dd compare as text.
"2026-11-04" >= "2026-11-02"istrue, sorun.date >= week && run.date <= sundaypicks out a week.addDays(week, 6)gives the Sunday. - Find the default folder from the script, not the terminal. Test 16 runs
log.jsfrom another folder. Paths and Folders showspath.join(import.meta.dirname, "data"), andpath.resolvegives the full path for the error message. - A default for the folder. Settings from Environment Variables shows
process.env.RUNLOG_DIR ?? .... - Errors and exit codes. Arguments, Exit Codes and Clear Errors sends errors to
console.errorand setsprocess.exitCode = 1. Return after an error, so nothing else is printed. - A missing file is ENOENT. Reading and Writing Files catches it and checks
error.code.
When you're done
Every test is green, and the command answers like this (each command, then what it printed):
node log.js --runner Priya --week 2026-11-02 --out summary.json
Priya's week of 2026-11-02
Runs: 2
Total: 13.0 miles
Average pace: 9:09 per mile
Longest run: 8.0 miles
Saved the summary to summary.json
node log.js -r Zoe -w 2026-11-02
There's no runner named Zoe in the training log.
node log.js --runner Jonah --week 2026-11-09
Jonah has no runs in the week of 2026-11-09.
summary.json holds:
{
"runner": "Priya",
"week": "2026-11-02",
"runs": 2,
"totalMiles": 13,
"averagePace": "9:09",
"longestRun": 8
}
Now look back at log.js. If Dana asked for a --month option tomorrow, which parts of your code would change, and which would stay exactly as they are?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-node-training-log), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-node-training-log.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-node-training-log, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Read a file, work it out, write the answer, and fail honestly: that's the shape of every command-line tool, from a coach's weekly report to the build tools that run on every commit at work.