Skip to content
Exercise · Classes and prototypes · lesson 9 of 12

Exercise: bank account

Complete the class Account, a bank account. constructor(initial) opens an account with the balance initial, deposit(amount) puts money into it, and the getter balance returns the current balance. The method withdraw(amount) takes money out and returns true or false for whether it worked. The function function createAccount(initial) in the starter code is already finished and must stay: the tests create accounts through it.

  • createAccount(100).balance → 100
  • a = createAccount(50), then a.withdraw(20) → true, and a.balance → 30
  • after that, a.withdraw(40) → false, and a.balance is still 30

Constraints: amounts are non-negative integers; when amount exceeds the balance, withdraw returns false and changes nothing, but withdrawing the whole balance works; each account keeps its own balance.

Do not add extra imports unless the task says so explicitly.

bank-account.js — javascript
javascript · 20 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way