Home > front end >  How to get sum of transaction from an array of objects
How to get sum of transaction from an array of objects

Time:09-16

I'm trying to return the sum of all transactions but the program isn't returning the sum

question

Define a function, totalTransactions, that takes an array of objects called transactions.

totalTransactions should return the total amount spent on all transactions.

my code

function totalTransactions(transactions) {
  let sum = 0

  for (key in transactions) {
    transactions[key]  = sum;
  }

  return sum;
}

Given information This is a nested data structure. Similar to 2D arrays, we can use a combination of bracket and dot notation to access nested key-values pairs

For example, to access the first name and amount values, we could use the index of 0 (for the first list item) followed by the individual key values:

Given example code

let transactions = [
  {
    name: "Tons of glitter",
    amount: 70
  },
  {
    name: "Porcelain Pink Flamingos",
    amount: 92
  },
  {
    name: "Chandelier replacement",
    amount: 10000,
  },
  {
    name: "Dinner at TGIF x6",
    amount: 350
  }
];

lastFridayNight(transactions) // => 10512

CodePudding user response:

Since you are computing an aggregate, reduce is your friend here.

Start with a sum equal to the number 0. At each intermediate step, add the amount of the current transaction to the aggregate.

let transactions = [
  {
    name: "Tons of glitter",
    amount: 70
  },
  {
    name: "Porcelain Pink Flamingos",
    amount: 92
  },
  {
    name: "Chandelier replacement",
    amount: 10000,
  },
  {
    name: "Dinner at TGIF x6",
    amount: 350
  }
];

function totalTransactions(transactions) {
  return transactions.reduce((sum, tx) => sum   tx.amount, 0);
}

console.log(totalTransactions(transactions));

CodePudding user response:

To help understand, your data is an array of Objects. So, we can use index notation to iterate through each Object, but then we use dot notation to access the value of a property of that object.

"forEach" is a function of Array object, it is a nice shorthand over "for( let i =0, i , i < t.length ){ t[i] do code here}"

You can access properties of Objects in a few ways: https://dmitripavlutin.com/access-object-properties-javascript/

So, we are iterating through each "item" of the transaction list and accessing the 'amount' property, adding it to a function scoped ( via let ) variable "sum" and then returning that value.

function totalTransactions(t) {
  let sum = 0;
  t.forEach(item => {
    sum  = item.amount;
  })
  return sum;
}

Another way to access the property, similar notation to arrays:

function totalTransactions(t) {
  let sum = 0;
  t.forEach(item => {
    sum  = item['amount'];
  })
  return sum;
}

CodePudding user response:

You're using for..in on the array, try for..of. May I see an example array that would be used an the totalTransactions parameter? Also, im sure you should switch around the reassignment like this sum = transactions[key]; so tha tsum is reasigned and not the object.

  • Related