ProgramGuru

Expense Tracker

Expense Tracker — purpose and prerequisites

An expense tracker records amount, category, and date, then aggregates by category or month. Financial applications should choose a consistent unit such as integer minor currency units to avoid binary floating-point rounding problems.

What you’ll learn

  • Explain the purpose of expense tracker.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The totals function accumulates integer paise per category. Formatting divides by 100 only for display. This avoids repeated fractional addition during aggregation.

Storing monetary amounts as ordinary fractions and repeatedly adding values such as 0.1 can produce surprising binary floating-point results.

Visualize the flow

Expense Tracker — learning flowExpense entries → Category totals → Spending reportExpense Tracker1Expense entries2Category totals3Spending reportProject core logic is executable; integrations may need setup.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

type Expense = { category: string; paise: number };
function totals(items: readonly Expense[]): Map<string, number> {
  const result = new Map<string, number>();
  for (const item of items) result.set(item.category, (result.get(item.category) ?? 0) + item.paise);
  return result;
}
const expenses: Expense[] = [{ category: "food", paise: 12950 }, { category: "travel", paise: 2500 }, { category: "food", paise: 5050 }];
for (const [category, paise] of totals(expenses)) console.log(`${category}: ₹${(paise / 100).toFixed(2)}`);

Verified output

food: ₹180.00
travel: ₹25.00

Compile: npx tsc --strict --target es2022 --module commonjs expense-tracker.ts; run: node expense-tracker.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The totals function accumulates integer paise per category. Formatting divides by 100 only for display. This avoids repeated fractional addition during aggregation.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Storing monetary amounts as ordinary fractions and repeatedly adding values such as 0.1 can produce surprising binary floating-point results.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Add dates and a monthly filter. Reject negative expenses and report the highest-spending category.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes expense tracker in the example?
Choose one answer. You can try again.

Answer and explanation

The totals function accumulates integer paise per category.

The totals function accumulates integer paise per category. Formatting divides by 100 only for display. This avoids repeated fractional addition during aggregation.

Key idea to remember

The totals function accumulates integer paise per category. Formatting divides by 100 only for display. This avoids repeated fractional addition during aggregation.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Quiz Application →