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

Library / My reading notes

Read the implementation and find boundary errors

Trace input to output and repair a concrete defect.

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

Chapter 2 / 3 · Read and take notes

Trace input to output and repair a concrete defect.

Understand the method

Review validation, calculation and return values in order. The draft below works for some inputs but uses >= and accepts negative, fractional or overflowing values. One successful example proves only that case. Map every rule to implementation and a test.

Describe the exact failure before changing code: expected 10000 for 5000,2, received 9000. This constrains the task better than “optimize the code.” Keep the failing test and the corrected result as evidence of what changed.

  1. 1Validate
  2. 2Calculate
  3. 3Check overflow
  4. 4Apply rule

Included material

function fee(price, count) {
  const total = price * count;
  return total >= 10000 ? total - 1000 : total;
}

Work through the steps

  1. Run the exact-threshold case and record the actual output.
  2. Validate inputs and subtotal, then correct the comparison.
  3. Explain each branch and confirm no network requests or dependencies were added.

Worked example

function fee(price, count) {
  if (!Number.isSafeInteger(price) || price < 0 ||
      !Number.isSafeInteger(count) || count < 1) {
    throw new RangeError("Invalid fee input");
  }
  const total = price * count;
  if (!Number.isSafeInteger(total)) throw new RangeError("Overflow");
  return total > 10000 ? total - 1000 : total;
}

Your exercise

Explain why count="2" should be rejected instead of relying on implicit JavaScript conversion.

Check before delivery

  • You can name the input that distinguishes >= from >.
  • You can distinguish input validation from overflow validation.
Finish the exercise, then view the reference

The interface expects integer values. Accepting a string hides an upstream format error. Number.isSafeInteger("2") is false, so an explicit rejection makes the defect easier to locate.

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