DEV Community

EddySantos07
EddySantos07

Posted on

newbie question help

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
}

Top comments (2)

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
}