0
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 getAllWithdrawals(bankAccounts) {
  let newArr = [];
  for (let acc of bankAccounts) {
    if (acc.withdrawals) {
      newArr.push(acc.withdrawals)
    } else if (!acc.withdrawals) {
      newArr.push(0);
    }
  }
  return newArr;
}

I am getting access to the array objects. But how do I get into the objects with the array of withdrawals with varying amounts, add them all and print that in the blank array "newArr"? Do I need another for loop? My overall goal is to iterate through the objects check which ones pass that have withdrawals array. If they do not have a withdrawals array I pass 0. The objects that do have withdrawals I need to iterate through those and add them up and push the total of the withdrawal array into the "newArr".

4
  • 1
    newArr.push(...acc.withdrawals) Commented Feb 7, 2023 at 19:19
  • Can you provide an example of what you would like the output to look like for the accounts given? Commented Feb 7, 2023 at 19:22
  • newArr = withdrawals.slice(0) Commented Feb 7, 2023 at 19:22
  • @NetByMatt I added some more text to clarify what I need a little better. Commented Feb 7, 2023 at 19:43

2 Answers 2

2

Here is a functional programming solution that uses map reduce:

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 getAllWithdrawals(bankAccounts) {
  return bankAccounts.map(obj => {
    return obj.withdrawals ? obj.withdrawals.reduce((sum, num) => sum + num, 0) : 0;
  });
}

console.log(getAllWithdrawals(bankAccounts));

Output:

[
  300.68,
  0,
  5900,
  0,
  100
]

Docs:

Here is an enhanced version where you pass the deposits or withdrawls key into the function:

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 getSums(bankAccounts, key) {
  return bankAccounts.map(obj => {
    return obj[key] ? obj[key].reduce((sum, num) => sum + num, 0) : 0;
  });
}

console.log({
  deposits: getSums(bankAccounts, 'deposits'),
  withdrawals: getSums(bankAccounts, 'withdrawals'),
});

Output:

{
  "deposits": [
    401,
    1100,
    24456.57,
    0,
    118
  ],
  "withdrawals": [
    300.68,
    0,
    5900,
    0,
    100
  ]
}

UPDATE 1: Based on request to use only for loops:

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 getAllWithdrawals(bankAccounts) {
  let result = [];
  for (let obj of bankAccounts) {
    let sum = 0;
    if(obj.withdrawals) {
      for (num of obj.withdrawals) {
        sum += num;
      }
    }
    result.push(sum);
  }
  return result;
}

console.log(getAllWithdrawals(bankAccounts));

Sign up to request clarification or add additional context in comments.

5 Comments

can only use for loops
@OldsoulPhil: What is the reason to only use for loops?
trying to teach younger brother some things and I don't know to much about code. I know more than him, but not a lot.
@OldsoulPhil: Gotcha. See UPDATE 1
Thank you. Me and my brother appreciate you. I can help him better with his homework now.
0

Not sure if I understood your question, but if u have to sum ALL of the withdrawals you should do it in this way:

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 getAllWithdrawals(bankAccounts) {
  let newArr = [];
  for (let acc of bankAccounts) {
    if (!!acc.withdrawals) {
    acc.withdrawals.forEach(withdrawal => newArr.push(withdrawal))
    }
  }
  return newArr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}

console.log(getAllWithdrawals(bankAccounts))

Otherwise if you have to sum the withdrawals of the single object you have to use this code:

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 getAllWithdrawals(bankAccounts) {
  let newArr = [];
  for (let acc of bankAccounts) {
    if (!!acc.withdrawals) {
        newArr.push(acc.withdrawals.reduce((accumulator, currentValue) => accumulator + currentValue, 0))
    }
  }
  return newArr;
}

console.log(getAllWithdrawals(bankAccounts))

1 Comment

can only use for loops

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.