Last active
July 6, 2019 23:15
-
-
Save proudlygeek/da6e0f55ecb476619051 to your computer and use it in GitHub Desktop.
Calculating with functions Codewars Kata
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
// 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; | |
}; | |
} |
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
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); |
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
Thank you for this. Was in the middle of trying to figure this out and was not even thinking of having another function like
recur
.