Skip to content

Instantly share code, notes, and snippets.

@fitsum
Last active April 26, 2024 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitsum/d60e6ebb3676347b5eb7ee9195c9a0d2 to your computer and use it in GitHub Desktop.
Save fitsum/d60e6ebb3676347b5eb7ee9195c9a0d2 to your computer and use it in GitHub Desktop.
Add, subtract, multiply, divide - practical JS closure/currying
maths = function(opp, a) {
checkNum = function(num) {
if (typeof num !== 'number') {
throw "Error: Quiting because '" + num + "' isn't a number";
}
}
checkNum(a);
return function(b) {
[a, b].forEach(function(n) {
checkNum(n);
});
return eval([b, opp, a].join(""));
}
}
//TODO: ES?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment