Build the checker
By the end of this step you'll have a working program, on your own computer, that lists the shortest descriptions with their word counts and reading times.
You now have every piece. This step puts them together: first in the browser, then on your machine.
The plan
Before writing code, say in plain words what it should do. Developers call this pseudocode:
- Read every row of
descriptions.csv. - For each row, count the words in the description.
- Work out the reading time: words ÷ 200 (people read about 200 words a minute).
- Keep the SKU, the word count and the reading time together as a record, in a list of results.
- Sort the results, shortest first.
- Print the first 10.
Steps 1–4 you've done in the last steps. Step 5 is new.
The one new thing: sorting
Output
Press Run (or Ctrl+Enter) to see the output here.
Output
Press Run (or Ctrl+Enter) to see the output here.
In Python, sorted(results, key=lambda r: r["words"]) reads: sort the results, using each record's word count. (lambda r: r["words"] is a tiny one-line function: given a record r, return its word count.) In JavaScript, results.sort((a, b) => a.words - b.words) compares two records at a time.
1. Write it in the browser
Start from the file reader in the box below and follow the plan. Write it yourself, one step of the plan at a time, pressing Run after each one to check it still works. Stuck on a step? Ask your AI tutor to explain that step, not to write the program.
Output
Press Run (or Ctrl+Enter) to see the output here.
Output
Press Run (or Ctrl+Enter) to see the output here.
When it works, click Copy on your box. Your code is now on your clipboard.
2. Move it to your computer
Now you'll run it for real. You need Get set up steps 1–4 done.
a. Make a folder for it, inside your work folder:
On Windows
mkdir ~\ridgeline\description-checkerOn macOS / Linux
mkdir -p ~/ridgeline/description-checker(If you haven't made the ridgeline folder yet, this makes it too.)
b. Open that folder in VS Code: File → Open Folder…, go to your home folder, then ridgeline, then description-checker, and click Select Folder (Mac: Open).
c. Add the data. Download descriptions.csv and move it into the description-checker folder. It appears in VS Code's file list on the left.
d. Create your program file. In VS Code: File → New File…, and name it:
checker.pyfor Python, orchecker.mjsfor JavaScript. (.mjstells Node this file usesimport.)
Paste your code into it.
e. JavaScript only: change how the file is read. In the browser the file came in through a special import. On your computer, Node reads it from the folder. Replace the first line (import csvText from "./descriptions.csv.js";) with these two lines:
import { readFileSync } from "node:fs";
const csvText = readFileSync("descriptions.csv", "utf8");f. Save the file (Ctrl + S, Mac: Cmd + S). A white dot on the file's tab means "not saved yet".
g. Run it. Open VS Code's terminal (Terminal → New Terminal). It's already in the right folder. Then:
On Windows
python checker.pyOn macOS / Linux
python3 checker.pyor, for JavaScript:
node checker.mjsIf it didn’t work
"No such file or directory: 'descriptions.csv'" / "ENOENT". The program looks for the file in the folder the terminal is in. Run ls: you should see both checker.py (or checker.mjs) and descriptions.csv. If not, move the CSV into this folder, or cd into the folder that has both.
"can't open file 'checker.py'". Same cause: you're in a different folder, or the file isn't saved yet.
JavaScript: "Cannot use import statement outside a module". Your file is called .js. Rename it to checker.mjs.
JavaScript: "Failed to resolve module './descriptions.csv.js'". You missed part e.
An error that works fine in the browser. Read the last line of the error message, then paste the whole error and your code into your AI chat and ask "explain this error to a beginner".
Your turn
The ten shortest are all empty, which hides the ones that are merely short. Change your program so it skips empty descriptions in the top 10, but prints a line at the end saying how many empty ones there were.