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→100a = createAccount(50), thena.withdraw(20)→true, anda.balance→30- after that,
a.withdraw(40)→false, anda.balanceis still30
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.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.