Created
September 2, 2019 07:20
-
-
Save LeanSeverino1022/dd15b50aaf3e88f8805408ca495d62ad to your computer and use it in GitHub Desktop.
get average value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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