I have a form with multiple rows where a user can input numbers and calculate outputs.
I can currently calculate the output of each row - the sub-total. But would like to be able to calculate all sub-totals - the total.
I'm able to log each sub-total to the console, but cannot figure out how to sum those values. Having read some similar questions I reckon I need to create an array of the values and sum those values.
Is this possible using the loop I'm currently using?
Here's what I currently have:
const rows = document.querySelectorAll('.row');
const calculate = document.querySelector('.calculate');
const output = document.querySelector('.output');
// Calculate
function calculateSubTotal() {
rows.forEach(function(row, index){
const input1Val = Number(row.querySelector('.input-1').value);
const input2Val = Number(row.querySelector('.input-2').value);
const subTotal = input1Val + input2Val;
console.log(subTotal);
});
}
calculate.addEventListener('click', calculateSubTotal);
Here's a pen: https://codepen.io/abbasarezoo/pen/WBXjWO?editors=1111