Explorer
JavaScript

Write a function `createBankAccount(initialBalance)` that returns an object with `deposit(amount)`, `withdraw(amount)`, and `getBalance()`. Withdrawals should fail (return false) if insufficient funds.

Problem Statement

<p>Write a function <code>createBankAccount(initialBalance)</code> that returns an object with <code>deposit(amount)</code>, <code>withdraw(amount)</code>, and <code>getBalance()</code>. Withdrawals should fail (return false) if insufficient funds.</p>

Examples

Input: const acc = createBankAccount(100); acc.deposit(50); acc.getBalance();

Output: 150

Explanation: Balance is safely maintained within the closure scope.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Use a closure to keep the balance private. šŸ’” Hint 2: withdraw should check if amount <= balance before deducting. šŸ’” Hint 3: Return false from withdraw if insufficient, true if successful.

Editorial & Approach

Problem Overview & Intuition

To solve Private Variables with Closures, we consider the execution characteristics of JavaScript engines. Write a function `createBankAccount(initialBalance)` that returns an object with `deposit(amount)`, `withdraw(amount)`, and `getBalance()`. Withdrawals should fail (return false) if insufficient funds. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (use a closure to keep the balance private).
  3. Implement Logic: Handle state and transformations efficiently (withdraw should check if amount <= balance before deducting).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function createBankAccount(initialBalance) {
  let balance = initialBalance;
  return {
    deposit(amount) { balance += amount; return true; },
    withdraw(amount) {
      if (amount > balance) return false;
      balance -= amount;
      return true;
    },
    getBalance() { return balance; }
  };
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

Private Variables with Closures

Medium

Write a function createBankAccount(initialBalance) that returns an object with deposit(amount), withdraw(amount), and getBalance(). Withdrawals should fail (return false) if insufficient funds.

Example Scenarios
1Example 1
Input: const acc = createBankAccount(100); acc.deposit(50); acc.getBalance();
Output: 150
Explanation:

Balance is safely maintained within the closure scope.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[120,true,false]