Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build a Calculator - Calculating averages
// 1/11/2016
Basic JS - Calculating averages
// Let's build a calculator that can calculate the average for an arbitrary
// number of arguments.
// The test expects you to provide a Calculator object with an average method:
// Calculator.average()
// The test also expects that when you pass no arguments, it returns 0.
// The arguments are expected to be integers.
// It expects Calculator.average(3,4,5) to return 4.
var Calculator = {
average : function () {
var arr = Array.prototype.slice.call(arguments)
if (arr.length===0) return 0
else return arr.reduce(function(a,b){return a+b},0) / arr.length
}
}
Calculator.average(3,4,5,6,7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.