I am trying to make a budget tracker. I have been researching how to go about doing it and found some code here that I studied, used and modified a bit in my project.
I have been using array .reduce and .map to get the final sum of all the inputs but struggling with how to use final numbers to then subtract to each other and get a ‘final balance’.
I tried to create a let variable to assign to the final sums but every time, and then subtracted them from each other however, it either returns as NaN or 0.
let totalIncome = incomeTotal.value;
let totalExpense = total.value;
//total income
income.forEach(x =>
addEventListener("input", (e) => {
incomeTotal.value =
Array.from(income)
.map((x) => x.value)
.reduce((accumulator, currentValue) => +accumulator + +currentValue, 0);
//expense
total.value =
Array.from(input)
.map((x) => x.value)
.reduce((accumulator, currentValue) => +accumulator + +currentValue, 0);
//final
finalValue.value = (+totalIncome - +totalExpense);
}));