Get average value of items in a javascript array

PHOTO EMBED

Sun Jan 05 2020 19:39:47 GMT+0000 (Coordinated Universal Time)

Saved by @bezequi #javascript #webdev #arrays #math

const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length
// arrAvg([20, 10, 5, 10]) -> 11.25
content_copyCOPY

In this function, we first use reduce() to reduce all values in our array to a single sum. Once we have the sum we simply divide this value by the length of the array. The result is the average value which then gets returned.

https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332