Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Last active July 6, 2019 23:15
Show Gist options
  • Save proudlygeek/da6e0f55ecb476619051 to your computer and use it in GitHub Desktop.
Save proudlygeek/da6e0f55ecb476619051 to your computer and use it in GitHub Desktop.
Calculating with functions Codewars Kata
// http://www.codewars.com/kata/525f3eda17c7cd9f9e000b39/train/javascript
function recur(n, op) { return (op) ? op.call(op, n) : n; }
function zero(op) { return recur(0, op); }
function one(op) { return recur(1, op); }
function two(op) { return recur(2, op); }
function three(op) { return recur(3, op); }
function four(op) { return recur(4, op); }
function five(op) { return recur(5, op); }
function six(op) { return recur(6, op); }
function seven(op) { return recur(7, op); }
function eight(op) { return recur(8, op); }
function nine(op) { return recur(9, op); }
function plus(num) {
return function(res) {
return res + num;
};
}
function minus(num) {
return function(res) {
return res - num;
};
}
function times(num) {
return function(res) {
return res * num;
};
}
function dividedBy(num) {
return function(res) {
return res / num;
};
}
Test.assertEquals(one(plus(one())), 2);
Test.assertEquals(two(times(three())), 6);
Test.assertEquals(seven(times(five())), 35);
Test.assertEquals(four(plus(nine())), 13);
Test.assertEquals(eight(minus(three())), 5);
Test.assertEquals(six(dividedBy(two())), 3);
@seonatic
Copy link

seonatic commented Jun 24, 2018

Nice but I got errors on the divide operations fixed with:

function dividedBy(num) {
return function(res) {
return Math.floor(res / num);
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment