AI MOOC
✦ Grow togetherInvite 3 friends, earn 1 month of membership · Up to 12 monthsInvite friends →

Library / My reading notes

Specify behavior with acceptance examples

Keep implementation within a clear scope and avoid treating one successful run as proof.

Back to course plan: Build a small feature you can verify with AI

Chapter 1 / 3 · Read and take notes

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.

  1. 1Rules
  2. 2Manual examples
  3. 3Implement
  4. 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

  1. Write rules for valid ranges, the discount boundary and errors.
  2. Calculate the three examples manually; add zero attendees and a fractional price.
  3. 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.

References and further reading

The lessons are original. The following sources are provided under their respective permissions.

Course

Mastering GitHub Copilot

Microsoft / Anthony Bartolo · en/zh

MIT

License checked. This entry links to the original; the book text is not mirrored.

License checked: 2026-10-16 · Pinned 49137b58
Course

Generative AI with JavaScript

Microsoft · en/zh

MIT

License checked. This entry links to the original; the book text is not mirrored.

License checked: 2026-10-16 · Pinned 03e41953