0 points

Get average value of items in a javascript array


dashboard

Sun Jan 05 2020 19:39:47 GMT+0000 (UTC)

Posted 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_copy

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



LISTS WITH THIS SNIPPET

>> Browse more code snippets

more_vert