I'm working on a project where the user adds numbers to an array and then, on a click event, the sum of those values is returned. However, the "total" button only ever returns 0 for my "total" variable. Just can't figure out the prob I set up the total variable based on what I saw here: how to add array element values with javascript?
var times = [];
var total = 0;
for (var i = 0; i < times.length; i++) {
total += +times[i];
}
document.getElementById("add").onclick = function () {
times.push(1);
};
document.getElementById("total").onclick = function () {
alert(times + " adds up to " + total);
};
Here's my fiddle
Aucun commentaire:
Enregistrer un commentaire