I'm having an issue with handling a simple personalAccount object in JavaScript. I have defined some functions within the object keys, but when I try to display the output of my accountBalance function using console.log, it doesn't show the expected result.
Within the accountSummary key, I am calculating the difference between totalIncome and totalExpense to get the desired output, but instead of getting the correct result, I am seeing NaN (Not a Number) in the terminal.
`
const personAccount = {
firstName: 'Prashant',
lastName: 'Singh',
incomes: [20000, 30000, 40000],
expenses: [],
totalIncome: function() {
return this.incomes.reduce((acc, curr) => acc + curr, 0);
},
totalExpense: function() {
return this.expenses.reduce((acc, curr) => acc + curr, 0);
},
accountInfo: function () {
return `First Name: ${this.firstName}, Last Name: ${this.lastName}, Total Income: ${this.totalIncome()}, Total Expense: ${this.totalExpense()}, Account Balance: ${this.totalIncome() - this.totalExpense()}`
},
addIncome: function (income) {
this.incomes.push(income);
return this.incomes;
},
addExpense: function (expenses){
this.expenses.push(expenses);
return this.expenses;
},
accountBalance: function () {
return this.totalIncome() - this.totalExpense();
},
accountSummary: function () {
return `First Name: ${this.firstName}, Last Name: ${this.lastName}, Balance: ${this.accountBalance()}`
}
}
`