Ledger reconciliation: safe integer amounts
Project milestone
Compare two small single-currency ledgers using integer cents. Validate IDs and amounts at the boundary; reject arithmetic that cannot be represented exactly as a safe JavaScript integer.
Run the examples
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {checkedAdd}=require('./ledger.cjs');console.log(checkedAdd(1200,-250));Expected console output:
950The result is 950 cents, not 950 major currency units.
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {checkedAdd}=require('./ledger.cjs');try{checkedAdd(Number.MAX_SAFE_INTEGER,1);}catch(e){console.log(e.name);}Expected console output:
RangeErrorThe result is outside the safe integer range.
Implementation and contract
checkedAdd() checks both operands and the sum with Number.isSafeInteger. indexEntries() accepts up to 1000 entries, each with a unique bounded ID and a safe integer amount. Negative cents represent reversals in this teaching model. Currency conversion, taxes and rounding rules are outside the contract.
Common mistakes
Do not add binary floating-point major-unit amounts and treat the result as exact money. Do not assume safe operands guarantee a safe sum.
Acceptance task
Test the upper safe-integer boundary, a negative adjustment and a string amount that must be rejected.
Project source, setup and tests: projects/ledger-reconcile/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ledger reconciliation: safe integer amounts — Knowledge Check
Assess implementation decisions and project behavior.
What does cents represent?
Why validate the sum as well as the operands?
What does a negative amount mean here?
In the Ledger reconciliation: safe integer amounts lab, what does this independent example log? const {checkedAdd}=require('./ledger.cjs'); console.log(checkedAdd(1200,-250));
In the Ledger reconciliation: safe integer amounts lab, what does this independent example log? const {checkedAdd}=require('./ledger.cjs'); try{checkedAdd(Number.MAX_SAFE_INTEGER,1);}catch(e){console.log(e.name);}