Last active
August 8, 2016 21:02
-
-
Save safhac/17bf38942b6f8017e37f3d3814968d6a to your computer and use it in GitHub Desktop.
calculator closure
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 calculator(operator) { | |
function func(a, b) { | |
return eval([a, operator, b].join('')); | |
}; | |
return func; | |
} | |
var multiply = calculator('*'); | |
var subtract = calculator('-'); | |
var add = calculator('+'); | |
var divide = calculator('/'); | |
alert(multiply(2, 3)); | |
alert(subtract(2, 3)); | |
alert(add(2, 3)); | |
alert(divide(2, 3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment