Content
You are given the following code that manages a bank account using plain objects and functions:
function createAccount(owner, balance) {
return { owner, balance, transactions: [] };
}
function deposit(account, amount) {
if (amount <= 0) throw new Error("Deposit amount must be positive");
account.balance += amount;
account.transactions.push({ type: "deposit", amount, date: new Date().toISOString() });
}
function withdraw(account, amount) {
if (amount <= 0) throw new Error("Withdrawal amount must be positive");
if (amount > account.balance) throw new Error("Insufficient funds");
account.balance -= amount;
account.transactions.push({ type: "withdrawal", amount, date: new Date().toISOString() });
}
function getStatement(account) {
return account.transactions
.map(t => `${t.date} | ${t.type} | ${t.amount} | Balance: ${account.balance}`)
.join("\\n");
}
// Usage
const acc = createAccount("Alice", 100);
deposit(acc, 50);
withdraw(acc, 30);
console.log(getStatement(acc));
BankAccount class.owner and balance should be set via the constructor. transactions is initialized as an empty array.deposit(amount), withdraw(amount), and getStatement() become methods.balance and transactions private using # private fields. Expose balance through a getter.const alice = new BankAccount("Alice", 100);
const bob = new BankAccount("Bob", 50);
alice.deposit(200);
bob.withdraw(20);
console.log(alice.balance); // 300
console.log(alice.getStatement());
console.log(bob.balance); // 30
console.log(bob.getStatement());
Build a small task management system using two classes.
Class Task
title (string) and priority ("low", "medium", "high").done, initialized to false.complete() that sets done to true.toString() that returns a string like: [HIGH] Buy groceries (done) or [LOW] Read book (pending).Class TaskManager
#tasks array.add(title, priority) — creates a Task and adds it to the list. Returns the created task.complete(title) — finds a task by title and marks it done. Throws an error if not found.listPending() — returns an array of all tasks that are not done.listByPriority(priority) — returns tasks filtered by the given priority.summary() — returns an object: { total: 5, done: 2, pending: 3 }.The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

Built with ❤️ by the HackYourFuture community · Thank you, contributors
Found a mistake or have a suggestion? Let us know in the feedback form.