Full-Stack course / JavaScript fundamentals
Packing summary
How to fetch and submit this exercise
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitPacking 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:
| Box | Holds |
|---|---|
fullBoxes | 2 |
leftover | 3 |
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 = ...;andlet perBox = ...;at the top of the file. The tests replace the numbers on them.itemsis a whole number (it can be 0) andperBoxis a whole number of at least 1. fullBoxesandleftoverare numbers.labelsis one string with the labels separated by a comma and a space, with nothing before the first label or after the last one.- Build
labelswith aforloop. You'll need aletbox to collect the text, then you can store the finished text inlabels. summaryalways saysboxes, even when there is exactly one box:"1 full boxes, 0 left over".- Use arithmetic operators, not
Mathanything. Don't usevar. - To look at your boxes while you work, add
console.log(fullBoxes, leftover, labels, summary);at the bottom and runnode packing_summary.js.
What you'll practice
- Remainder and division working together
- Template literals for sentences
- A
forloop 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
- The remainder operator
%tells you how many items are left over after as many boxes as possible have been filled. - Take the leftover away from
itemsand what remains fills whole boxes exactly, so dividing it byperBoxnever leaves a fraction. For the labels, aforloop can count boxes from 1 up tofullBoxes, adding to a text box on each lap. - Start a
letbox with an empty string. On each lap addBox ${box}to it, and add,first on every lap except the first. When the loop is done,labelsis that finished text.