I'm struggling to design a loop that will cycle through an array of arrays and add each number together, work out the average then output to the console.
Here is my code;
var data = [
[3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95],
[100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93],
[6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80],
[6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97],
[6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88],
[6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99]
]
var calcTotal, arrayTotal, totalSum;
calcTotal = [];
arrayTotal = [];
totalSum = [];
arrayTotal.push(data[0])
totalSum = data[0].reduce(function(a, b) {
return a + b;
});
calcTotal.push(totalSum)
console.log(Math.round(totalSum / 12))
http://plnkr.co/edit/Ses4XApKEdo2CCZmsis7?p=preview
So far I have it working to display just one result, Ideally I would output the average from each array when added together in a single array to the console.
I've been playing around with for/forEach loops but can't seem to crack it, if anyone can offer some help/advice?
Thanks
function sum(a,b){return a+b;} averages = data.map(function(s){return s.reduce(sum,0)/s.length;}); globalAverage = averages.reduce(sum,0)/averages.length;- Something like that, perhaps?