Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Bank Account

Object-Oriented Programmingeasy

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Model a bank account that rejects overdrafts. Implement `BankAccount` (starting balance 0): - `deposit(amount)` adds funds and returns true. - `withdraw(amount)` returns false if it would overdraw, otherwise subtracts and returns true. - `getBalance()` returns the current balance.

Examples

Input: deposit(100), withdraw(30), withdraw(200), getBalance()
Output: [true, false, 70]
The 200 withdrawal is rejected (insufficient funds), leaving 70.

Sample tests

Input: deposit then withdraw, overdraft rejected
Output: [true,false,70]
Input: exact withdrawal allowed
Output: [true,0]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Allowing a withdrawal that drives the balance negative.
Approach & explanation (try first)

Encapsulating the balance and validating withdrawals enforces the overdraft rule.

Loading...
⌘/Ctrl + Enter

Run your code to see results.