Exercise: The Daily Specials
Print The Daily Grind's chalkboard of daily specials with console.log, fix a line with a mistake in it and switch a sold-out item off with a comment, and let the tests check every line.
Novice · JavaScript · Web Development · Updated
The Daily Grind writes its specials on a chalkboard by the door every morning, and the owner wants the same board printed by a program, so it can go on a screen by the counter. Your job is to finish that program: one console.log line for each item on the board. One line is already written but has a mistake in it, and one item has sold out. Tests run your program and check every line it prints, so you'll know when the board is right.
Get the exercise code
In the Exercise box beside the lesson, choose Get the starter on GitHub to open its GitHub repository. Click Code, then Download ZIP, and unzip it into your javascript-course folder.
The folder holds:
specials.js: the chalkboard program. The comments list each special. For you to finish.specials.test.js: the tests that run your program and read what it prints. You don't need to change this file.package.json: tells Node.js how to run this folder's code and its tests. Ready to use.
In VS Code, choose File > Open Folder and open the exercise's folder, then open the terminal with Terminal > New Terminal.
What to do
Each problem in specials.js is a comment with an empty line or two under it. Two examples at the top show the format:
// Example i. The shop's name.
console.log("i) Shop: The Daily Grind");
// Example ii. The day this chalkboard is for.
console.log("ii) Today: Tuesday");
i) Shop: The Daily Grind
ii) Today: Tuesday
Every line you print has the same shape: the problem number, ), the item's name, : , then its price or its value.
- Problems 1 to 6: print one line for each special, under its comment, such as
1) Pumpkin Spice Latte: $5.25. - Problem 7: a barista started this line, and it has a mistake. Run the file, read the error message, and fix the line.
- Problem 8: the banana bread sold out. Turn its line into a comment, so it stays in the file for tomorrow but doesn't print today.
- Problem 9: print the happy hour line, as the comment shows.
Check your work
Run the program from the exercise folder:
node specials.js
The first time, it stops with an error and prints nothing: that's problem 7 at work. Then check every line with the tests:
node --test
At first all 11 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 has an error in it, every test fails with the same 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. Copy one, paste it under the next comment, and change the words.
- Read the error from the top. The first line names the file and the line number; the line that starts with an error's name, such as
SyntaxError, says what kind of problem it is. Your First JavaScript Program walks through one. - Count the quotes and parentheses. Text starts and ends with a double quote, and every
(needs its). - Two slashes switch a line off.
//at the start of a line turns it into a comment. In VS Code, Ctrl+/ (Cmd+/ on macOS) does it for you. - Run it often. Run
node specials.jsafter every line or two you add, so a mistake is never far from where you're working.
When you're done
Every test passes, and the program prints the whole chalkboard:
i) Shop: The Daily Grind
ii) Today: Tuesday
1) Pumpkin Spice Latte: $5.25
2) Maple Cold Brew: $4.95
3) Hot Chocolate: $3.75
4) Blueberry Muffin: $3.25
5) Breakfast Burrito: $6.75
6) Soup of the day: Tomato Basil
7) Pastry of the day: Cinnamon Roll
9) Happy hour: 2 PM to 4 PM
The banana bread is back tomorrow. How would you put it back on the board, and what would you change if the shop raised its price to $3.75?
So what
You wrote a program, ran it, read a real error message and fixed it, and let tests tell you when the work was right. That loop of write, run, read the message and check is how every exercise in this course works, and how professional developers work every day.
Key terms
- console.log()
- Comment
- node Command
- Syntax Error