Home > database >  Use For loop to iterate through array of objects to get the greatest Balance
Use For loop to iterate through array of objects to get the greatest Balance

Time:02-05

const bankAccounts = [
  {
    id: 1,
    name: "Susan",
    balance: 100.32,
    deposits: [150, 30, 221],
    withdrawals: [110, 70.68, 120],
  },
  { id: 2, name: "Morgan", balance: 1100.0, deposits: [1100] },
  {
    id: 3,
    name: "Joshua",
    balance: 18456.57,
    deposits: [4000, 5000, 6000, 9200, 256.57],
    withdrawals: [1500, 1400, 1500, 1500],
  },
  { id: 4, name: "Candy", balance: 0.0 },
  { id: 5, name: "Phil", balance: 18, deposits: [100, 18], withdrawals: [100] },
];

function getClientWithGreatestBalance(bankAccounts) {
  const maxAccount = bankAccounts[0];
  const newArr = [];
  for (let i = 0; i < array.length; i  ) {
    if (bankAccounts[i].balance > maxAccount.balance)
    newArr.push([i]);
  }
  return newArr;
}

I am trying to loop through the bankAccounts array to get the object that has the greatest balance and push that object into the new array and return that single account. I know I need to check the balance of each account but for some reason can not figure out how to get the single object.

CodePudding user response:

You can't know which is the biggest until you've seen all of them, so it doesn't make sense to push anything to your output array until the end.

Instead, you need to keep track of the account with the biggest balance of those you've seen so far. So instead of an array, you want a single variable to track that one.

In fact you already have that variable, maxAccount. So if you find an account with a bigger balance than maxAccount.balance, you want to set maxAccount to the new account, instead of the previous max.

Once you have finished the loop, then you can push that to your output array (assuming you even need an output array, since you are always just returningg one).

Other points about your code:

getClientWithGreatestBalance will crash if you give it an empty input array.

You are starting by assuming (temporarily) that the first account is the biggest - which is OK, but then you are starting a loop at the first account, so the first time round the loop is comparing the first account to itself, which is unnecessary (but won't cause any errors).

It's better to iterate over an array directly rather than by index. Again, your code isn't wrong in that respect, but over-complicated.

for (let account of array) {
    // ...
}

CodePudding user response:

I guess that array variable you are using in the for loop is meant to be bankAccounts, and at first it seemed weird to me that you are returning an array then I thought that you may have wanted to return an array in case there are multiple accounts that share the max balance. So, with that in mind your function should be something like this

function getClientWithGreatestBalance(bankAccounts) {
  let maxBalance = 0;
  let maxBalanceAcounts = [];
  for (let i = 0; i < bankAccounts.length; i  ) {
     if (bankAccounts[i].balance > maxBalance){
        maxBalance = bankAccounts[i].balance;
        maxBalanceAcounts = [];
        maxBalanceAcounts.push(bankAccounts[i])
     }
     else if (bankAccounts[i].balance === maxBalance){
        maxBalanceAcounts.push(bankAccounts[i])
     }
  }
  return maxBalanceAcounts;
}
  • Related