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`.
Try it yourself
Compare each loan request against the map lookup and clearance rules.
| Case | Result |
|---|---|
| stockMap = { "OSC-1": 2 }, itemCode = "OSC-1", clearance = true | Select a choice above to see what happens. |
| stockMap = { "OSC-1": 2 }, itemCode = "LASER-9", clearance = true | Select a choice above to see what happens. |
| stockMap = { "OSC-1": 2 }, itemCode = "", clearance = true | Select a choice above to see what happens. |
Reveal the explanation
- stockMap = { "OSC-1": 2 }, itemCode = "OSC-1", clearance = true
Approve (true). Item is in stock (2 > 0) and student has clearance.
- stockMap = { "OSC-1": 2 }, itemCode = "LASER-9", clearance = true
Decline (false). Missing key safely defaults to 0 stock instead of crashing.
- 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.
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
This browser could not save your progress. Changes last until you leave the page.