I'm currently developing a bank account program and facing a challenge in adding my direct debits (DDs) into the global variable. When I create new objects using my constructor function and add them into the bank account, only the last created DD is showing up.
My aim is to push all DDs into the bank account.
Here's the code snippet:
// Initializing the bank account with a value of 0
var bankAccount = {};
// Constructor function to create a direct debit with a name and cost
function DirectDebit(name, cost) {
this.name = name;
this.cost = cost;
}
// Creating a new direct debit for my phone
var phone = new DirectDebit("Phone", 20);
var car = new DirectDebit("Car", 250);
function addToBank(dd) {
bankAccount = dd;
}
addToBank(phone);
addToBank(car);
console.log(bankAccount);
This results in:
DirectDebit { name: 'Car', cost: 250 }