Exercise: Bakery Price Board
Finish Maple Street Bakery's price board, a Node program that prints each item with console.log, fixing a line with a mistake, switching a sold-out item off with a comment and printing process.version, then let the tests check every line and hand it in with Git.
Novice · Node.js · JavaScript · Updated
Maple Street Bakery writes its prices on a board by the door every morning, and the owner wants the same board printed by a program, so it can go on the screen by the counter. Your job is to finish that program: one console.log line for each item. One line has a mistake in it, one item has sold out, and one line shows the version of Node that runs the board. Tests run your program and check every line it prints, so you'll know when the board is right. This is the course's first exercise, so this lesson walks through every step, from getting the code to handing it in.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-node-bakery-price-board, 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-bakery-price-board.git
Git makes a folder, exercise-node-bakery-price-board, with the exercise code inside. If you can't clone it, click Code on the repository's page, then Download ZIP, and unzip it into your nodejs-course folder instead.
Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and run:
npm install
This exercise uses nothing but Node itself, so npm has nothing to download and says up to date. Every exercise starts with this step; later ones download the packages their tests use. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.
The folder holds:
price-board.js: the price board program, for you to finish. The comment at its top shows every line it should print.price-board.test.js: the tests, which run your program and read each line it prints. You don't need to change it.package.jsonandpackage-lock.json: the project's settings and its scripts,start,devandtest.
What to do
Each problem in price-board.js is a comment with an empty line or two under it. Two examples at the top show the format:
// Example i. The bakery's name.
console.log("i) Bakery: Maple Street Bakery");
// Example ii. The day this board is for.
console.log("ii) Day: Saturday");
i) Bakery: Maple Street Bakery
ii) Day: Saturday
Every line you print has the same shape: the problem number, ), the item's name, : , then its price or value.
- Problems 1 to 5: print one line for each item, under its comment, such as
1) Sourdough loaf: $6.50. - Problem 6: a baker started this line, and it has a mistake. Run the program, read the error message, and fix the line.
- Problem 7: the lemon tart sold out. Turn its line into a comment, so it stays in the file for tomorrow but doesn't print today.
- Problem 8: print
8) Node version:followed by the version of Node that runs the board, usingprocess.version, never the version typed by hand. - Problem 9: print the opening hours, as the comment shows.
Check your work
Run the program from the exercise folder:
npm start
npm start runs node price-board.js. The first time, it prints the two example lines and stops with an error: that's problem 6 at work. npm run dev runs it with node --watch instead, so it runs again each time you save. Then check every line with the tests:
npm test
At first all 10 tests fail. That's expected: each problem you finish turns its test green. A failing test's message says which problem it is and what it expected, for example Problem 3 printed nothing. Print a line like: 3) Item name: value. While the program stops with an error, every test fails with that error's message: fix the error first, and the rest start to pass one by one.
Hints
- Copy the pattern. Every line you write is shaped like the two examples, with different words.
- Read the error from the top. The first line names the file and the line number, and the line that starts with the error's name says what went wrong. Your First Node.js Program reads one with you. Capital letters count in JavaScript.
- Two slashes switch a line off.
//at the start of a line turns it into a comment. In VS Code, Ctrl+/ (Cmd+/ on a Mac) does it for you. console.logtakes several values. Separated by commas, they print on one line with a space between each, as Your First Node.js Program shows withconsole.log("Loaves left:", 14).processknows about Node. Your First Node.js Program names whatprocess.versionholds.
When you're done
Every test passes, and npm start prints the whole board, with your own Node version on line 8:
i) Bakery: Maple Street Bakery
ii) Day: Saturday
1) Sourdough loaf: $6.50
2) A dozen bagels: $12.00
3) Cinnamon roll: $3.75
4) Blueberry muffin: $3.25
5) Baguette: $4.00
6) Bread of the day: Rye
8) Node version: v24.19.0
9) Open: 7 AM to 2 PM
The lemon tart is back tomorrow. Which one change brings it back, and what else would you change if its price went up to $4.75?
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-bakery-price-board), 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. If you cloned the starter, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-node-bakery-price-board.git git add . git commit -m "My solution" git push -u origin mainIf you downloaded the ZIP instead:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-node-bakery-price-board.git git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-node-bakery-price-board, 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
You cloned a project, ran it, read a real error and fixed it, let tests tell you when the work was right, and handed it in with Git. Every exercise in this course follows that same loop, and so does a developer's working day.