Reputation: 13
First function produces a random number. Each time the second function is called it should output the total number of array values added together. why does this only output one variable value?
function random() {
return Math.floor((Math.random() * 10) + 1);
}
var addtime = [];
function totalArray() {
var newCell3 = random();
var Value = '';
var total = 0;
value = newCell3;
addtime.push(value);
for (var i in addtime) {
total += addtime[i];
}
console.log(total);
}
totalArray();
Upvotes: 0
Views: 58
Reputation: 31825
I guess this is what you are looking for:
const random = () => Math.floor((Math.random() * 10) + 1);
var addTime = [];
document.getElementById('button').addEventListener('click', totalArray);
function totalArray() {
const newValue = random();
let total;
addTime.push(newValue);
total = addTime.reduce((x, y) => x + y, 0);
console.log(total);
}
<button id="button">Click Me!</button>
Upvotes: 2