I've been attempting to calculate the total sum of account balances for accounts labeled as "Savings", but my current approach seems to be unsuccessful. My initial plan was to iterate through the array using a forEach loop and then check if the account type is classified as savings, however I'm encountering some issues.
const accounts = [
{acctNo: 123, type: 'Checking', balance: 150},
{acctNo: 234, type: 'Checking', balance: 200},
{acctNo: 345, type: 'Savings', balance: 550},
{acctNo: 456, type: 'Checking', balance: 550},
{acctNo: 567, type: 'Savings', balance: 1500}
];
const sum = accounts.forEach(function(account){
if (account.type === 'Savings'){
account.reduce(function(a,b){
return {balance: a.balance + b.balance};
});
}
});
console.log(sum);