How to get sum of transaction from an array of objects

Viewed 78

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
3 Answers

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));

EDIT

I like the way the above snippet works and would probably go that route if it was my project. However, if the goal here is to fix your code in the simplest way possible, here is an alternative solution.

function totalTransactions(transactions) {
  let sum = 0

  for (const tx of transactions) {
    sum += tx.amount;
  }

  return sum;
}

There are 2 main changes here:

  1. Changing your loop from for .. in to for .. of iterates over the transactions, not the keys in the array.
  2. This line transactions[key] += sum; does not make much sense. You are adding the value of sum (always 0 in this case) to an object. My test shows it coercing both values to a string a concatenating them. This is definitely not what you want! Instead, you want to get the value amount from each transaction and add it to sum.

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;
}

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, I'm sure you should switch around the reassignment like this sum += transactions[key]; so that sum is reasigned and not the object.

Related