How can I add all the mybag
array values into total_w
where mybag
has mixed values of float
or int
?
var mybag = [];
mybag[0] = 20.50;
mybag[1] = 10.13;
mybag[3] = 0;
//so total_w should be 30.63 not: 20.5010.13
var total_w = 0;
var comma = '';
for (key in mybag) {
active_mybag = active_mybag + comma + parseFloat(mybag[key]).toFixed(2);
total_w = Math.round(total_w + parseFloat(mybag[key]).toFixed(2));
comma = ",";
}
console.log('> ', total_w, active_mybag);