Keep implementation within a clear scope and avoid treating one successful run as proof.
Understand the method
Define inputs and business rules before asking for code. Our event fee accepts an integer price in minor units and an integer attendee count. Price may be zero; count must be at least one. Apply a 1000-unit discount only when the subtotal exceeds 10000, not when it equals 10000. Integer minor units avoid decimal addition problems.
Derive boundary tests from words such as zero, at least and exceeds. Require a defined error type and keep scope small: no payments, accounts or UI. This makes unnecessary additions visible and keeps a teaching function distinct from a payment system.
- 1Rules
- 2Manual examples
- 3Implement
- 4Verify
Included material
Specification: fee(price, count) returns integer minor units. Invalid input throws RangeError. 5000×2 → 10000; 5001×2 → 9002; 0×1 → 0.
Work through the steps
- Write rules for valid ranges, the discount boundary and errors.
- Calculate the three examples manually; add zero attendees and a fractional price.
- Give the rules and tests to an assistant and request an explanation of the implementation.
Worked example
Implement a pure JavaScript fee(price, count). Use integer minor units and Number.isSafeInteger. Require price >= 0, count >= 1 and a safe-integer subtotal. Subtract 1000 only if subtotal > 10000. Throw RangeError for invalid values. Add no dependencies or requests. List boundary examples before implementation.
Your exercise
Add a test for subtotal overflow and explain why validating inputs alone is insufficient.
Check before delivery
- The exact threshold and the value above it are separate cases.
- Invalid input has an observable error behavior.
Finish the exercise, then view the reference
Multiplying safe integers can still exceed the safe range, such as Number.MAX_SAFE_INTEGER times 2. Check Number.isSafeInteger(total) after multiplication.
An original course with teaching exercises. Reading records, practice outcomes and independent certification are distinct.