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.
- 1Validate
- 2Calculate
- 3Check overflow
- 4Apply rule
Included material
function fee(price, count) {
const total = price * count;
return total >= 10000 ? total - 1000 : total;
}Work through the steps
- Run the exact-threshold case and record the actual output.
- Validate inputs and subtotal, then correct the comparison.
- 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.