Skip to content

Instantly share code, notes, and snippets.

@LeanSeverino1022
Created September 2, 2019 07:20
Show Gist options
  • Select an option

  • Save LeanSeverino1022/dd15b50aaf3e88f8805408ca495d62ad to your computer and use it in GitHub Desktop.

Select an option

Save LeanSeverino1022/dd15b50aaf3e88f8805408ca495d62ad to your computer and use it in GitHub Desktop.
get average value
//Returns the average of all values in any non-empty array.
//note that the '0' is an optionally supplied initial value. If this value is not supplied, the 0th element is used as the initial value.
const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length
// arrAvg([20, 10, 5, 10]) -> 11.25
/*
notes: Finding the average value of a group of numbers is as easy as summing all of the numbers and dividing by the total number of numbers.
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.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment