Write createAccount(owner, opening) that keeps the balance private and exposes deposit, withdraw (refusing overdrafts) and statement methods.
function createAccount(owner, opening = 0) {
let balance = opening; // only the methods below can reach it
const entries = [];
function record(text) { entries.push(text); }
return {
deposit(amount) { balance += amount; record(`+${amount}`); return balance; },
withdraw(amount) {
if (amount > balance) { record(`refused ${amount}`); return balance; }
balance -= amount; record(`-${amount}`); return balance;
},
statement() { return `${owner}: ${entries.join(" ")} | balance ${balance}`; },
};
}
const acc = createAccount("Asha", 1000);
acc.deposit(500);
acc.withdraw(2000);
acc.withdraw(300);
console.log(acc.statement());
console.log(acc.balance, typeof balance);
Asha: +500 refused 2000 -300 | balance 1200 undefined undefined
balance and entries live in createAccount's scope. The returned methods close over them; nothing outside can read or change the balance directly (acc.balance is undefined).