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
- Deposit 100, withdraw 30: the balance is 70
- Setting
balancefrom outside doesn't change it - The balance isn't visible in keys or JSON
- Withdrawing too much throws a RangeError and keeps the balance
- Depositing a negative amount throws a RangeError
- 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.