-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.
– Augusto Vasques
What is the intention with
const result = sum + transaction.value.length
? Everything seemed fine up to that point. It’s not enough to printsum
in the end?– bfavaretto