The var declaration inside the loop body is hoisted to function scope, so item and lineTotal outlive each iteration.
Exorcism strategy: Declare loop-scoped values with const inside the loop body and use let only where reassignment is needed.
Implicit coercionHEX
Séance Chamber
Approval: Approved on 2026-11-07
Requirements
Requirements: Invoice totals calculator
Acceptance Criteria
THE computeInvoiceTotal function SHALL return an object with the numeric fields subtotal, discount, tax, and total.
WHEN an item has a quantity of 0, THE computeInvoiceTotal function SHALL exclude that item from the subtotal.
WHEN the coupon is HALF, THE computeInvoiceTotal function SHALL set the discount to half of the unrounded subtotal.
WHEN the coupon is TEN, THE computeInvoiceTotal function SHALL set the discount to ten percent of the unrounded subtotal.
IF the coupon is any other value, THENTHE computeInvoiceTotal function SHALL set the discount to 0.
THE computeInvoiceTotal function SHALL compute the tax as the discounted subtotal multiplied by taxRate and SHALL round every returned amount to 2 decimal places.
THE computeInvoiceTotal function SHALL NOT read or modify any global state.
Design
Design: Invoice totals calculator
Entry function: computeInvoiceTotal(order).
Input: an object { items, taxRate, coupon }. items is an array of { price: number, quantity: number }, taxRate is a number where 0.2 means 20 percent, and coupon is a string.
Laboratory
Verified
Verified behavioral equivalence across 1,000 generated inputs.
This is empirical evidence from randomly generated inputs. Other inputs were not checked.
Completed in 555 ms.
Target stack: TypeScript 6 (strict mode, ES2022). Created .
Loose equality on the coupon code allows values of other types to match the string and hides invalid input.
Exorcism strategy: Use strict equality on a string-typed coupon field, or a lookup table of known coupon codes.
Output: an object { subtotal, discount, tax, total } whose fields are numbers rounded to two decimal places.
The function is pure and deterministic. The TypeScript version declares the input and output shapes as interfaces, replaces loose equality with strict equality, and keeps the arithmetic order unchanged.