Course outline
Full-Stack course / JavaScript fundamentals

Basket summary

Lab

How to fetch and submit this exercise

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each exercise: download it, edit the file named in the instructions, then submit.

gonomicon fetch fs-week-2/02-03-basket-summary
cd 02-03-basket-summary
gonomicon submit

The runner depends on the exercise: JavaScript/TypeScript/React labs run with Vitest, Python labs with pytest, and config-file labs (Docker, CI, Terraform, Nginx) are checked by parsing your file. gonomicon test runs the visible checks on your computer first; submitting also runs the hidden checks on our server.

Basket summary

Goal: price a shopping basket with three functions: one with a default parameter and two that accept any number of prices through a rest parameter.

Worked example

CallReturns
withTax(100)120
withTax(80, 25)100
withTax(50, 0)50
sumAll(5, 10, 20)35
sumAll()0
basketLine("Gifts", 10, 20, 30)"Gifts x3 = 60"
basketLine("Snacks")"Snacks x0 = 0"
basketLine()"Basket x0 = 0"
basketLine(undefined, 5, 10)"Basket x2 = 15"

The three functions

  • withTax(price, percent) gives back the price plus percent percent of the price. If nobody says what the percent is, use 20.
  • sumAll(...amounts) gives back the total of every number it was given, or 0 when it was given none.
  • basketLine(label, ...prices) gives back a line of text: the label, an x, how many prices there were, an equals sign and the total of the prices. If nobody gives a label, use "Basket". Passing undefined as the label means "I'm not giving one".

Rules

  • Write all three as function declarations, with exactly these names.
  • withTax must use a default parameter. sumAll and basketLine must use a rest parameter, and must not use the old arguments object.
  • Each function must return its answer rather than print it.
  • Walk the list of prices with a loop.
  • You can try things out with a line like console.log(basketLine("Gifts", 10, 20, 30)); at the bottom while you work, then remove it, and run node basket_summary.js.

What you'll practice

  • A default parameter, and what it takes to trigger one
  • A rest parameter collecting leftover arguments into a list
  • Walking that list with for...of while keeping a running total
  • Building one line of text with a template literal

Run it

gonomicon fetch fs-week-2/02-03-basket-summary
cd 02-03-basket-summary
gonomicon test
gonomicon submit

Hints

  1. For withTax, put the default right in the parameter list: percent = 20. For the other two, a rest parameter (...amounts, ...prices) hands you every extra argument as a list you can walk with for...of.
  2. Inside sumAll, make a let box that starts at 0 and add each amount to it in a for...of loop. basketLine needs the same total plus a count of how many prices came in, so keep two boxes going in one loop. The label slot comes first, so give it a default and put the rest parameter after it.
  3. function withTax(price, percent = 20) { return price + price * percent / 100; }. For the other two: function sumAll(...amounts) { let total = 0; for (const amount of amounts) { total += amount; } return total; }, and basketLine works the same way with a counter, ending in return ${label} x${count} = ${total};.