Library Problem

You have $50 in your wallet. You must pay a $3 fine for each late book (b). Then, a friend gives you $10. Write an expression for your remaining money.

$50
Start Amount
Initial Money
-
Why "-"?Paying fine reduces money
$3
×
b
Fine Cost
Subtracted per book
+
Why "+"?Receiving money adds to total
$10 Gift
Added to wallet
=
50 - 3b + 10
Final Expression

Calculating Remaining Money

Sometimes expressions involve multiple operations. Let's track money flowing in and out of a wallet.
Continue

1. Starting Amount

You begin with $50. This is your initial positive balance.
Continue

2. The Late Fines (-)

You have to PAY $3 for each late book (b). Since money is leaving your wallet, we SUBTRACT this cost: -3b.
Continue

3. The Gift (+)

Your friend GIVES you $10. Since money is entering your wallet, we ADD this amount: +10.
Continue

4. Final Expression

Combine it all in order: Start - Fine + Gift. Expression: 50 - 3b + 10.
Continue

Your Turn

Practice combining subtractions and additions to track a changing balance.
Continue

Wallet Function

Functions can manage state changes like transactions.

calculateWallet.ts
function calculateRemaining(lateBooks: number): number {
  const startMoney = 50; 
  const finePerBook = 3;
  const gift = 10;
  
  // Expression: 50 - 3b + 10
  // Note: Parentheses prioritize the multiplication
  return startMoney - (finePerBook * lateBooks) + gift;
}

// 5 late books:
console.log(calculateRemaining(5)); 
// 50 - (3 * 5) + 10
// 50 - 15 + 10 = 45
Transaction Logic
AlgoAnimator: Interactive Data Structures