Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculator in num1(operation(num2())); format, just for fun.
// Link to kata: http://www.codewars.com/kata/calculating-with-functions/javascript
// How it works:
// seven(times(five())); // returns 35
// four(plus(nine())); // returns 13
// eight(minus(three())); // returns 5
// six(dividedBy(two())); // returns 3
function zero() {
if (arguments.length == 0) return 0;
return calc(0, arguments[0]);
}
function one() {
if (arguments.length == 0) return 1;
return calc(1, arguments[0]);
}
function two() {
if (arguments.length == 0) return 2;
return calc(2, arguments[0]);
}
function three() {
if (arguments.length == 0) return 3;
return calc(3, arguments[0]);
}
function four() {
if (arguments.length == 0) return 4;
return calc(4, arguments[0]);
}
function five() {
if (arguments.length == 0) return 5;
return calc(5, arguments[0]);
}
function six() {
if (arguments.length == 0) return 6;
return calc(6, arguments[0]);
}
function seven() {
if (arguments.length == 0) return 7;
return calc(7, arguments[0]);
}
function eight() {
if (arguments.length == 0) return 8;
return calc(8, arguments[0]);
}
function nine() {
if (arguments.length == 0) return 9;
return calc(9, arguments[0]);
}
function calc(n, op) {
var num = n;
switch(arguments[1][0]) {
case 'plus': return num + arguments[1][1];
case 'minus': return num - arguments[1][1];
case 'times': return num * arguments[1][1];
case 'dividedBy': return num / arguments[1][1];
}
}
function plus(num) {return ['plus', num]}
function minus(num) {return ['minus', num]}
function times(num) {return ['times', num]}
function dividedBy(num) {return ['dividedBy', num]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.