Perform sum of value to insert into an object

Asked

Viewed 42 times

-2

I have the challenge of a course to solve, but I’m having difficulties. Link to the challenge.

With the code below, I was able to insert the objects with the types credit and Debit in the transactions of user. I was also able to list only objects with type credit. However, I do not know how to accomplish the sum of the values of credit and then insert into the property swing. Is it also that I am following the right way to resolve?

// Crie um programa para realizar operações bancárias na conta de um usuário.

// Comece criando um objeto com o nome do usuário, suas transações e saldo.

const user = {
  name: "Mariana",
  transactions: [],
  balance: 0
};

// O type pode ser credit para créditos e debit para débitos da conta do usuário.
createTransaction({type: 'credit', value: 50.5})
createTransaction({type: 'credit', value: 80})
createTransaction({type: 'debit', value: 40})
createTransaction({type: 'debit', value: 30})

// Crie uma função createTransaction
function createTransaction(transaction) {
  //adicionar uma nova transação no array de transações de um usuário
  user.transactions.push(transaction)
}

// Quanto uma transação for do tipo credit ela deve também somar o valor do crédito no saldo (balance) do usuário.
for (let transaction of user.transactions) {
  const credit = transaction.type === 'credit'
  let sum = 0

  if (credit) {
    sum = sum + transaction.value
    const result = sum + transaction.value.length
    console.log(result)
   
  }
}
  • Could you edit the question and be clear in explaining what it is, what the proleme is and where the problem is.

  • What is the intention with const result = sum + transaction.value.length? Everything seemed fine up to that point. It’s not enough to print sum in the end?

1 answer

0


Its function createTransaction should also be concerned with updating the balance user with the new value just entered instead of having to iterate the array each time to update the value, you could do something like:

const user = {
  name: "Mariana",
  transactions: [],
  balance: 0
};

createTransaction({type: 'credit', value: 50.5})
createTransaction({type: 'credit', value: 80})
createTransaction({type: 'debit', value: 40})
createTransaction({type: 'debit', value: 30});

console.log(user);

function createTransaction(transaction) {
  user.transactions.push(transaction);
  
  if(transaction.type === 'credit')
    user.balance += transaction.value;
}

This way each time you enter a new transaction, the balance value will be updated together with the entered transaction, but if you just want to enter the transactions and only then perform the code calculation you can do as follows:

const user = {
  name: "Mariana",
  transactions: [],
  balance: 0
};

createTransaction({type: 'credit', value: 50.5})
createTransaction({type: 'credit', value: 80})
createTransaction({type: 'debit', value: 40})
createTransaction({type: 'debit', value: 30});

updateBalance();
console.log(user);

function createTransaction(transaction) {
  user.transactions.push(transaction);
}

function updateBalance(){
  for(let transaction of user.transactions){
    if(transaction.type === 'credit')
      user.balance += transaction.value; // += é a mesma coisa que ter user.balance = user.balance + transation.value;
  }
}

With this second example you can enter as many transactions as you want and only then call the function updateBalance which calculates the value of the based on existing transactions in the array.

  • Thanks for the answer Leo, it helped me a lot. I’m a beginner in both Javascript and the stackoverflow community and may not have asked the question correctly. But the doubt was very well clarified by you. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.