Command Palette

Search for a command to run...

Hectal
Phase 7: this, Prototypes and Classes
JavaScript · Exercise 4 of 5 PreviousNext
Intermediate

A bank account with a private balance

Rewrite BankAccount so the balance is private (#balance) and can only change through deposit(amount) and withdraw(amount). Outside code can read it through a getter balance, but can't set it.

deposit and withdraw must throw a RangeError for amounts that aren't positive. withdraw must throw a RangeError with a message containing Not enough money if the amount is more than the balance, and leave the balance unchanged.

The buttons are already wired up: they show the balance, or the error message in #message.

What Check tests

  1. Deposit 100, withdraw 30: the balance is 70
  2. Setting balance from outside doesn't change it
  3. The balance isn't visible in keys or JSON
  4. Withdrawing too much throws a RangeError and keeps the balance
  5. Depositing a negative amount throws a RangeError
  6. The page: deposit 50, then withdrawing 80 shows the error and keeps 50

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.