Created
December 25, 2011 15:51
-
-
Save sri-rang/1519450 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript - 7
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
const CIRCLE = "circle"; | |
const SQUARE = "square"; | |
const RECTANGLE = "rectangle"; | |
var getAreaFunction = function (shape) { | |
return function () { | |
switch (shape) { | |
case CIRCLE: | |
return Math.PI * arguments[0] * arguments[0]; | |
break; | |
case SQUARE: | |
return arguments[0] * arguments[0]; | |
break; | |
case RECTANGLE: | |
return arguments[0] * arguments[1]; | |
break; | |
} | |
}; | |
}; | |
var getAreaOfCircle = getAreaFunction(CIRCLE); | |
var getAreaOfSquare = getAreaFunction(SQUARE); | |
var getAreaOfRectangle = getAreaFunction(RECTANGLE); | |
console.log(getAreaOfCircle(50)); | |
console.log(getAreaOfSquare(50)); | |
console.log(getAreaOfRectangle(50, 20)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment