|
| 1 | +// This time we want to write calculations using functions and get the results. Let's have a look at some examples: |
| 2 | + |
| 3 | +// seven(times(five())); // must return 35 |
| 4 | +// four(plus(nine())); // must return 13 |
| 5 | +// eight(minus(three())); // must return 5 |
| 6 | +// six(dividedBy(two())); // must return 3 |
| 7 | + |
| 8 | +// Requirements: |
| 9 | + |
| 10 | +// There must be a function for each number from 0 ("zero") to 9 ("nine") |
| 11 | +// There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy |
| 12 | +// Each calculation consist of exactly one operation and two numbers |
| 13 | +// The most outer function represents the left operand, the most inner function represents the right operand |
| 14 | +// Division should be integer division. For example, this should return 2, not 2.666666...: |
| 15 | + |
| 16 | +// eight(dividedBy(three())); |
| 17 | + |
| 18 | +// Solution:five(): The five function is called, and it returns the number 5. So, at this point, the expression becomes seven(times(5));. |
| 19 | + |
| 20 | +// times(5): The times function is called with the argument 5. The times function returns a new function that multiplies its argument by 5. So, at this point, the expression becomes seven([function that multiplies by 5]);. |
| 21 | + |
| 22 | +// seven(...): The seven function is called with the argument being the function returned by times(5). The seven function takes this function as an argument and returns the result of applying that function to the number 7. In other words, it's performing the multiplication by 5. |
| 23 | + |
| 24 | +function zero(n) { |
| 25 | + return n ? n(0) : 0; |
| 26 | +} |
| 27 | +function one(n) { |
| 28 | + return n ? n(1) : 1; |
| 29 | +} |
| 30 | +function two(n) { |
| 31 | + return n ? n(2) : 2; |
| 32 | +} |
| 33 | +function three(n) { |
| 34 | + return n ? n(3) : 3; |
| 35 | +} |
| 36 | +function four(n) { |
| 37 | + return n ? n(4) : 4; |
| 38 | +} |
| 39 | +function five(n) { |
| 40 | + return n ? n(5) : 5; |
| 41 | +} |
| 42 | +function six(n) { |
| 43 | + return n ? n(6) : 6; |
| 44 | +} |
| 45 | +function seven(n) { |
| 46 | + return n ? n(7) : 7; |
| 47 | +} |
| 48 | +function eight(n) { |
| 49 | + return n ? n(8) : 8; |
| 50 | +} |
| 51 | +function nine(n) { |
| 52 | + return n ? n(9) : 9; |
| 53 | +} |
| 54 | +function plus(a) { |
| 55 | + return function (b) { |
| 56 | + return b + a; |
| 57 | + }; |
| 58 | +} |
| 59 | +function minus(a) { |
| 60 | + return function (b) { |
| 61 | + return b - a; |
| 62 | + }; |
| 63 | +} |
| 64 | +function times(a) { |
| 65 | + return function (b) { |
| 66 | + return b * a; |
| 67 | + }; |
| 68 | +} |
| 69 | +function dividedBy(a) { |
| 70 | + return function (b) { |
| 71 | + return Math.floor(b / a); |
| 72 | + }; |
| 73 | +} |
| 74 | +console.log(seven(plus(five()))); |
| 75 | +console.log(seven(minus(five()))); |
| 76 | +console.log(seven(times(five()))); |
| 77 | +console.log(seven(divide(five()))); |
0 commit comments