Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active January 12, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmloveaa/023f185e256f576af09a to your computer and use it in GitHub Desktop.
Save mmloveaa/023f185e256f576af09a to your computer and use it in GitHub Desktop.
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