Tell us what’s happening:
This code should work. I’ve refreshed and reposted this code and am still failing test 21. And help is appreciated.
Your code so far
class BankAccount {
constructor(balance = 0, transactions = []) {
this.balance = balance;
this.transactions = transactions;
}
deposit(amount) {
if (amount <= 0) {
return "Deposit amount must be greater than zero.";
}
this.transactions.push({ type: "deposit", amount: amount });
this.balance += amount;
return `Successfully deposited $${amount}. New balance: $${this.balance}`;
}
withdraw(amount) {
if (amount <= 0 || amount > this.balance) {
return "Insufficient balance or invalid amount.";
}
// Store withdrawals as positive amounts with type "withdrawal"
this.transactions.push({ type: "withdrawal", amount: amount });
this.balance -= amount;
return `Successfully withdrew $${amount}. New balance: $${this.balance}`;
}
checkBalance() {
return `Current balance: $${this.balance}`;
}
listAllDeposits() {
const deposits = this.transactions
.filter(tx => tx.type === "deposit")
.map(tx => tx.amount);
return "Deposits: " + deposits.join(",");
}
listAllWithdrawals() {
const withdrawals = this.transactions
.filter(tx => tx.type === "withdrawal")
.map(tx => tx.amount); // amounts already positive
return "Withdrawals: " + withdrawals.join(",");
}
}
// === FreeCodeCamp Test Execution ===
let myAccount = new BankAccount();
console.log(myAccount.deposit(100)); // Deposit 1
console.log(myAccount.deposit(150)); // Deposit 2
console.log(myAccount.withdraw(50)); // Withdrawal 1
console.log(myAccount.withdraw(30)); // Withdrawal 2
console.log(myAccount.deposit(10)); // Deposit 3
console.log(myAccount.deposit(21)); // Deposit 4 - balance now > 100
console.log(myAccount.checkBalance());
console.log(myAccount.listAllDeposits());
console.log(myAccount.listAllWithdrawals());
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Challenge Information:
Build a Bank Account Management Program - Build a Bank Account Management Program