JavaScript / Editorial solution guide
Empty or invalid quantity becomes a misleading total
Trace text-to-number conversion, define an explicit quantity contract, and verify rejection without confusing it with a free item.
Read-only knowledge base · No public comments
Recognize the symptoms
A blank quantity produces a total of zero, invalid text produces NaN, or a fractional quantity reaches an order calculation. The issue can appear only for specific inputs even though ordinary numeric text works. Capture a synthetic input and the value immediately after conversion before changing the arithmetic.
Understand the likely causes
- Number converts empty or whitespace-only text to zero; other text may convert to NaN, and successful conversion does not guarantee a positive safe integer. A fallback such as value || 0 can further hide the distinction between rejected input and a legitimate zero price.
Reproduce the boundary
Start with the original text, not only the displayed total. Reproduce with an empty string, spaces, "3", "0", "1.5", and nonnumeric text. Record the input type and the converted value without logging a real customer order. Identify the first place the application turns a conversion into an assumption that the input is valid. If the input arrives from a non-form source, verify its type before applying string operations.
Define and implement the contract
For this example quantity is a nonempty positive safe integer. The function rejects empty text before conversion and rejects non-integers, unsafe integers, and nonpositive values after conversion. The tagged result keeps failure separate from a valid number. The caller must inspect ok before calculating; it should show an appropriate correction message rather than substitute a successful total. A different domain may legitimately allow zero, in which case change and document that rule.
function readQuantity(text) {
const trimmed = text.trim();
if (trimmed === "") return { ok: false, error: "required" };
const value = Number(trimmed);
if (!Number.isSafeInteger(value) || value < 1) {
return { ok: false, error: "invalid quantity" };
}
return { ok: true, value };
}
console.log(readQuantity(" 3 "));
console.log(readQuantity(""));
The caller receives either a validated safe integer or a specific rejection; empty input is never silently converted to a valid quantity.
Connect the result to the caller
Keep the original field text for correction and pass only the validated number to the calculator. Validate price independently as a nonnegative integer in the smallest currency unit. If the caller receives data over an API, repeat appropriate validation at the receiving boundary; client validation improves feedback but does not establish server trust. Consider product limits and safe arithmetic separately, since a safe quantity and a safe price can still have an unsafe product.
Verify the repair
Run the same input matrix after the change and confirm each rejected case follows the error branch without updating a successful order. Check a valid quantity with zero unit price still returns a valid zero total. Also test an unsafe integer and a product near the application’s supported limit. Record expected outcomes alongside the fixture so another developer can reproduce the decision, and confirm the caller displays the field-specific error without exposing private input in logs.
Verify the fix
Check blank, whitespace, positive integer, zero, fraction, invalid text and unsafe integer cases. Verify valid zero price separately and confirm the caller cannot compute on a rejected result.
Keep it from recurring
- Document units, accepted ranges, and rejection behavior at both interface and server boundaries. Retain a small table of valid and invalid examples in regression tests and avoid treating conversion as validation.