Curriculum

Curriculum

Build a Lab Equipment Loan Checker

Scenario

Contract `canLoanEquipment(stockMap, itemCode, hasSafetyClearance)`: `itemCode` must be a non-empty string and `hasSafetyClearance` boolean (else `INVALID_LOAN_INPUT`). Look up stock with default `0`; approve iff `hasSafetyClearance && stock > 0`.

Selected choicePick a choice below
Result—

Try it yourself

Compare each loan request against the map lookup and clearance rules.

Reveal the explanation
  1. stockMap = { "OSC-1": 2 }, itemCode = "OSC-1", clearance = true

    Approve (true). Item is in stock (2 > 0) and student has clearance.

  2. stockMap = { "OSC-1": 2 }, itemCode = "LASER-9", clearance = true

    Decline (false). Missing key safely defaults to 0 stock instead of crashing.

  3. stockMap = { "OSC-1": 2 }, itemCode = "", clearance = true

    Error: INVALID_LOAN_INPUT. Empty equipment code is invalid.

Treating an unknown equipment code as 0 stock prevents KeyError/undefined crashes while rejecting an empty string catches missing form fields early.

Review first
Download shared test cases

Your turn

Implement `canLoanEquipment` and verify all cases in `fixture-lab-equipment-loan`, including unknown key, zero stock, no clearance, and empty itemCode.

Common mistake Reading `stockMap[itemCode] > 0` without handling missing keys or negative stock entries.

Check your work

Function + passing test output for in-stock, zero-stock, unknown-key, no-clearance, and invalid-input cases.

Project brief

Basic
Implement map lookup with safe default `0` and clearance check.
Intermediate
Reject empty `itemCode`, non-object `stockMap`, or non-boolean clearance with `INVALID_LOAN_INPUT`.
Stretch
Add a `requestedQty` parameter and return a descriptive reason string alongside the boolean.

What to hand in

Include your diagram, expected-result table, code, actual test output, and the commit or diff you reviewed.

Review criteria

  • Logic · Your diagram shows every required path and boundary.
  • Verification · The tests exercise the stated acceptance cases and the recorded results match the code.
  • Review · The change is focused and you can explain the decision it implements.

Quick check

What should `canLoanEquipment({ "OSC-1": 2 }, "SCOPE-99", true)` return under this contract?
Show the answer and reasoning

Correct answer false (missing key defaults safely to 0 stock)

Safe lookup defaults unknown item codes to 0 stock, so `0 > 0` evaluates to `false` without crashing.