Menu
Coddy logo textTech

BankAccount Class

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 21 of 56.

challenge icon

Challenge

In this project we will create a BankAccount class from scratch to manage a customer's bank account.

Your first task is to create a class named BankAccount with:

  1. Constructor that takes two parameters:
    • accountHolder (string): The name of the account owner
    • initialBalance (number, optional): Starting balance (defaults to 0)
  2. Method <strong>getBalance()</strong> which:
    • Returns the current account balance
    • No parameters needed
  3. Method <strong>getAccountInfo()</strong> that:
    • Returns a formatted string: "${accountHolder}: ${balance}"
    • Example: If accountHolder is "John" and balance is 500, returns "John: $500"

Try it yourself

// TODO: Create a class named BankAccount

// TODO: Add a constructor that takes two parameters: accountHolder and initialBalance (defaults to 0)

// TODO: Add a method getBalance() that returns the current account balance

// TODO: Add a method getAccountInfo() that returns a formatted string: "${accountHolder}: $${balance}"


// Test
const testAccount = new BankAccount('Alex Johnson', 500);

console.log('Initial state:');
console.log(testAccount.getAccountInfo()); // "Alex Johnson: $500"
console.log('Balance:', testAccount.getBalance()); // 500

All lessons in Object Oriented Programming