Checkpoint: Inventory

The Course
•
4 min read

Your 15-minute checkpoint

Close the earlier examples. Spend two minutes writing the inputs, expected outputs, and steps in plain language. Use about eight minutes to build and test, then five to explain your choices and try the variation. Taking longer is normal: these are study budgets, not speed tests.

Use records, arrays, and deliberate copying. This problem deliberately changes the setting so you practise choosing the tools yourself.

A small warm-up

Before the main task, predict this familiar idea from today’s work. If it is unclear, revisit the relevant teaching lesson first.

Check the foundation

Say why each line appears, not just what it prints.

Check the foundation JavaScript
const item = { quantity: 2 };
const copy = { ...item, quantity: 3 };

console.log(item.quantity);
console.log(copy.quantity);
Check your prediction
2
3

Use this only as a warm-up. The main task needs you to combine several decisions.

Acceptance cases before you code

Write a small test plan before implementing packing data and copies. These cases extend the brief; they are not a second worked solution. Predict the expected behavior independently, then test through the public function. A good ordinary example and a good boundary example should catch different possible mistakes.

Case to tryWhat your result should demonstrate
Empty packing listZero pieces and no unpacked names
Packed item with quantity greater than oneCount pieces, not just records
Revised first recordOriginal packed flag and unrelated records remain unchanged

After the core task passes, deliberately break one rule and identify the case that notices it. Restore the implementation and explain the failure in terms of the contract, not the number of assertions. If a case requires a concept you have not recalled, revisit the relevant lesson before opening the solution.

Exercise

An equipment inventory

Each item has name, quantity, and packed. Count all pieces and list unpacked names. Then make a revised copy of the first record marked packed, leaving the original unchanged.

Your working space JavaScript
const items = [
  { name: "Notebook", quantity: 2, packed: false },
  { name: "Pen", quantity: 3, packed: true }
];
Hint 1: the idea

Use a number for the total and an array for the names.

Hint 2: a first step

Loop through items; add quantity and push name only when packed is false.

Show one solution
const items = [
  { name: "Notebook", quantity: 2, packed: false },
  { name: "Pen", quantity: 3, packed: true }
];
let pieces = 0;
const unpacked = [];

for (const item of items) {
  pieces += item.quantity;

  if (!item.packed) {
    unpacked.push(item.name);
  }
}

const revised = { ...items[0], packed: true };

console.log(pieces);
console.log(unpacked.join(", "));
console.log(items[0].packed);
console.log(revised.packed);
5
Notebook
false
true

Try two unpacked items. Then test the counting and name-listing part with no items; it should count zero. Skip making a copy of the first item when the list is empty, because there is no first record. Decide what text an empty unpacked list should display instead of a blank line.

Change the requirements

Try two unpacked items. Then test the counting and name-listing part with no items; it should count zero. Skip making a copy of the first item when the list is empty, because there is no first record. Decide what text an empty unpacked list should display instead of a blank line.

Decide what to revisit

  • Can you explain each variable and the reason for each condition?
  • Did you check an empty, boundary, or changed input rather than only the supplied one?
  • Can you rebuild the essential part after closing the solution?

If you needed a hint, record the specific missing idea and revisit that lesson. Marking a page done records progress; it does not replace practice. Begin tomorrow by rebuilding one small part from memory.

Was this article helpful?

Your response is saved on this device.