Last active
January 12, 2016 16:47
-
-
Save mmloveaa/023f185e256f576af09a to your computer and use it in GitHub Desktop.
Build a Calculator - Calculating averages
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
// 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