Course outline
Full-Stack course / JavaScript fundamentals

Packing 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/01-04-packing-summary
cd 01-04-packing-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.

Packing summary

Goal: work out how many full boxes a shipment fills and what is left over, then describe the result as a list of box labels and a sentence.

Worked example

The file starts with two boxes: items (how many things you have) and perBox (how many fit in one box). Your code creates four more.

With items = 23 and perBox = 10:

BoxHolds
fullBoxes2
leftover3
labels"Box 1, Box 2"
summary"2 full boxes, 3 left over"

With items = 4 and perBox = 10, nothing fills a box: fullBoxes is 0, leftover is 4, labels is the empty string "", and summary is "0 full boxes, 4 left over".

Rules

  • Keep the two lines let items = ...; and let perBox = ...; at the top of the file. The tests replace the numbers on them. items is a whole number (it can be 0) and perBox is a whole number of at least 1.
  • fullBoxes and leftover are numbers.
  • labels is one string with the labels separated by a comma and a space, with nothing before the first label or after the last one.
  • Build labels with a for loop. You'll need a let box to collect the text, then you can store the finished text in labels.
  • summary always says boxes, even when there is exactly one box: "1 full boxes, 0 left over".
  • Use arithmetic operators, not Math anything. Don't use var.
  • To look at your boxes while you work, add console.log(fullBoxes, leftover, labels, summary); at the bottom and run node packing_summary.js.

What you'll practice

  • Remainder and division working together
  • Template literals for sentences
  • A for loop that builds up text
  • Getting the first and last pieces of a list right

Run it

gonomicon fetch fs-week-2/01-04-packing-summary
cd 01-04-packing-summary
gonomicon test
gonomicon submit

Hints

  1. The remainder operator % tells you how many items are left over after as many boxes as possible have been filled.
  2. Take the leftover away from items and what remains fills whole boxes exactly, so dividing it by perBox never leaves a fraction. For the labels, a for loop can count boxes from 1 up to fullBoxes, adding to a text box on each lap.
  3. Start a let box with an empty string. On each lap add Box ${box} to it, and add , first on every lap except the first. When the loop is done, labels is that finished text.