Exercise: Cookies and Pizza
Plan a party in JavaScript. Write functions with default parameters for cookies and boxes, then use filter, map, find, sort and reduce with arrow functions to pick pizzas, price an order and describe it, with tests run by Node.
Apprentice · JavaScript · Updated
You're planning a party for 30 people, and JavaScript is doing the sums: how many cookies and boxes, how many pizzas, which are vegetarian, and what it all costs.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-cookies-and-pizza, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git. Then open the folder in VS Code (File > Open Folder), and open a terminal there (Terminal > New Terminal).
| File | What's in it |
|---|---|
cookies.js |
Three functions for you to finish |
pizza.js |
Six functions for you to finish |
party.js |
Plans a party with your functions |
cookies.test.js, pizza.test.js |
Tests for both files |
The requirements are in the comment at the top of cookies.js and pizza.js.
What to do
Run the tests with:
node --test
Node.js finds every file ending in .test.js and runs it. They all fail to start with. Finish the functions, running the tests as you go:
| File | Function | Does |
|---|---|---|
cookies.js |
cookiesNeeded(guests, perGuest = 2) |
Cookies for the guests |
boxesNeeded(cookies, boxSize = 12) |
Boxes, counting a part-full one | |
orderTotal(boxes, pricePerBox = 8.5) |
The price, 10% off for 5 or more boxes, rounded to cents | |
pizza.js |
pizzasNeeded(people, slicesEach = 3) |
Whole pizzas, at 8 slices each |
vegetarianNames(menu) |
The vegetarian pizzas' names | |
cheapest(menu, count) |
The cheapest pizzas' names, cheapest first | |
findPizza(menu, name) |
A pizza, whatever the capitals, or undefined |
|
orderPrice(menu, names) |
An order's total; an unknown pizza throws an Error |
|
describeOrder(names) |
A sentence such as 3 pizzas: Margherita, Pepperoni and Veggie |
Then plan the party with node party.js.
Hints
Math.ceilrounds up, so a part-full box or pizza still counts. To round to cents:Math.round(total * 100) / 100.- Defaults go in the parameter list, as in C#:
function cookiesNeeded(guests, perGuest = 2). filter,mapandfindtake arrow functions, just like LINQ'sWhere,SelectandFirstOrDefault.sortchanges the array it's called on, and one test checks that the menu keeps its order. Copy it first:[...menu]makes a new array with the same pizzas. Then sort the copy with(a, b) => a.price - b.price, andslice(0, count)takes the firstcount.reducecan add up the order;findPizzafinds each name's price.throw new Error(...)throws, like C#'sthrow new Exception(...).describeOrder:names.slice(0, -1)is every name but the last.
When you're done
Every test passes, and node party.js prints:
Cookies: 60, in 5 boxes, $38.25
Pizzas: 12 for 30 guests
Vegetarian: Margherita, Veggie, Garlic Bread
Cheapest two: Garlic Bread, Margherita
3 pizzas: Margherita, Pepperoni and Veggie, $37.00
There is no Hawaiian on the menu
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.
On GitHub, click +, then New repository. Name it (for example
exercise-cookies-and-pizza), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-cookies-and-pizza.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-cookies-and-pizza.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-cookies-and-pizza, 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
Functions with defaults, arrow functions, and array methods that are LINQ by another name: that's most of the JavaScript you'll write day to day. Next, you'll point it at a web page.