Ledger reconciliation: indexing and differences
Project milestone
Use Map to compare records by stable ID rather than their positions. Report missing, extra and changed entries separately, preserving the relevant input order.
Run the examples
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {reconcile}=require('./ledger.cjs');const r=reconcile([{id:'A',cents:0},{id:'B',cents:20}],[{id:'A',cents:0},{id:'C',cents:20}]);console.log(JSON.stringify([r.missing,r.extra,r.changed]));Expected console output:
[["B"],["C"],[]]Zero is present and the two distinct IDs produce missing and extra records.
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {indexEntries}=require('./ledger.cjs');try{indexEntries([{id:'A',cents:1},{id:'A',cents:2}]);}catch(e){console.log(e.message);}Expected console output:
Duplicate idA repeated ID is ambiguous and rejected.
Implementation and contract
indexEntries() rejects duplicate IDs rather than overwriting a financial record silently. reconcile() iterates the expected index for missing and changed IDs and the actual index for extra IDs. Use has() before get() because an amount of zero is valid. Under an assumed constant-time map operation cost, these passes are linear in the input sizes; engine internals are not benchmarked.
Common mistakes
Testing get(id) for truthiness misclassifies zero as absent. Reject duplicates before building the comparison.
Acceptance task
Compare ledgers with reversed order and identical IDs; no difference should be reported solely because order changed.
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: indexing and differences — Knowledge Check
Assess implementation decisions and project behavior.
How should matching entries be located?
Why use has() to detect presence?
What should duplicate IDs trigger?
In the Ledger reconciliation: indexing and differences lab, what does this independent example log? const {reconcile}=require('./ledger.cjs'); const r=reconcile([{id:'A',cents:0},{id:'B',cents:20}],[{id:'A',cents:0},{id:'C',cents:20}]); console.log(JSON.stringify([r.missing,r.extra,r.changed]));
In the Ledger reconciliation: indexing and differences lab, what does this independent example log? const {indexEntries}=require('./ledger.cjs'); try{indexEntries([{id:'A',cents:1},{id:'A',cents:2}]);}catch(e){console.log(e.message);}