DEV Community

newbie question help

EddySantos07 on May 29, 2019

what do i do now to find the average of a given array of numbers?

function avgValue (array) {
var sum = 0;
for (var i = 0; i <= array.length; i ++) {
sum += array[i]

}
return
}

Collapse
 
webdeasy profile image
webdeasy.de

You've got the sum of the values. Now all you have to do is divide them by the number:

return sum / array.length;

That's it! ;)

Collapse
 
montanacoder profile image
Jeremy

function avgValue (array=[]){
let sum = 0;
array.forEach(number => {
sum += number;
})
return sum/array.length
}