Created
September 25, 2019 17:21
-
-
Save niktariy/81827158bdf31bdff043ebca9bc316fe to your computer and use it in GitHub Desktop.
Function overloading
This file contains 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
function average() { | |
// the number of arguments passed | |
const length = arguments.length; | |
if (length == 0) return 0; | |
// convert the arguments to a proper array of numbers | |
const numbers = Array.prototype.slice.call(arguments); | |
// a reducer function to sum up array items | |
const sumReduceFn = function (a, b) { return a + Number(b) }; | |
// return the sum of array items divided by the number of items | |
return numbers.reduce(sumReduceFn, 0) / length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment