英文:
Why is the getTotals() method not working?
问题
I will only translate the code portion for you:
我正在创建一个财务应用程序,每插入一项数据,顶部的“收入”,“支出”和“余额”表根据用户输入的值进行更新。例如,如果代码从用户那里接收到收入值,那么“Receitas”和“Saldo”框必须更新并接收这些值。目前,代码无法正常工作。我应该怎么做?
normalizeNumbers(value) {
const newValue = value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL"
});
return newValue;
}
getTotals() {
const newReceitas = this.valores.filter(({ checked }) => {
checked === '<i class="fa-sharp fa-solid fa-arrow-up arrowUp-icon"></i>';
}).map(({ valor }) => +valor);
console.log(newReceitas);
const newDespesas = this.valores.filter(({ checked }) => {
checked == '<i class="fa-sharp fa-solid fa-arrow-down arrowDown-icon"></i>';
}).map(({ valor }) => +valor);
console.log(newDespesas);
const totalReceitas = newReceitas.reduce((acc, curr) => {
return acc + curr;
}, 0);
const totalDespesas = newDespesas.reduce((acc, curr) => {
return acc + curr;
}, 0);
const totalSaldo = +totalReceitas - +totalDespesas;
this.receitas.innerText = this.normalizeNumbers(+totalReceitas);
this.despesas.innerText = this.normalizeNumbers(+totalDespesas);
this.saldo.innerText = this.normalizeNumbers(totalSaldo);
}
Please note that I've translated the code portion as requested, and I haven't included any additional information.
英文:
I'm creating a finance app that, with each item that is inserted, the "income", "expenses" and "balance" tables at the top are updated according to the values entered by the user. For example, if the code receive a income value from the user, the "Receitas" and "Saldo" boxes have to be updated and receive these values. As it is, the code is not working. What should I do?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
normalizeNumbers(value) {
const newValue = value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL"
});
return newValue;
}
getTotals() {
const newReceitas = this.valores.filter(({ checked }) => {
checked === '<i class="fa-sharp fa-solid fa-arrow-up arrowUp-icon"></i>';
}).map(({ valor }) => +valor);
console.log(newReceitas);
const newDespesas = this.valores.filter(({ checked }) => {
checked == '<i class="fa-sharp fa-solid fa-arrow-down arrowDown-icon"></i>';
}).map(({ valor }) => +valor);
console.log(newDespesas);
const totalReceitas = newReceitas.reduce((acc, curr) => {
return acc + curr;
}, 0);
const totalDespesas = newDespesas.reduce((acc, curr) => {
return acc + curr;
}, 0);
const totalSaldo = +totalReceitas - +totalDespesas;
this.receitas.innerText = this.normalizeNumbers(+totalReceitas);
this.despesas.innerText = this.normalizeNumbers(+totalDespesas);
this.saldo.innerText = this.normalizeNumbers(totalSaldo);
}
}
<!-- end snippet -->
答案1
得分: 2
Looking at the code above, since I don't know the values of the valores
array, I presume it's because of the filter callbacks, which never return a value and return empty arrays.
Either add return
:
({ checked }) => {
return checked == '....'
}
or use an arrow function:
({ checked }) => checked == '....'
英文:
Looking at the code above, since I don't know the values of the valores
array, I presume it's because of the filter callbacks, which never return a value and return empty arrays.
Either add return
({ checked }) => {
return checked == '....'
}
or use arrow function
({ checked }) => checked == '....'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论