-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterger_calc.js
53 lines (46 loc) · 1.48 KB
/
interger_calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const Mathematics = {
sum: function sum(x, y) {
return x + y;
},
minus: function minus(x, y) {
return Mathematics.sum(x, y * -1);
},
product: function product(x, y) {
return Mathematics._calc_(x, y, 0, (x, y, result) => {
for (let i = 0; i < y; i++) { result = Mathematics.sum(result, x); };
return result;
});
},
divide: function divide(x, y) {
return Mathematics._calc_(x, y, 0, (x, y, result) => {
while (Mathematics.minus(x, y) >= 0) { result++; x = Mathematics.minus(x, y); };
return result;
});
},
power: function power(x, y) {
return Mathematics._calc_(x, y, 1, (x, y, result) => {
for (let i = 0; i < y; i++) { result = Mathematics.product(result, x) };
return result;
});
},
square: function square(x, y = 2) {
return Mathematics._calc_(x, y, 0, (x, y, result) => {
while (x != 1) { result++ ; x = Mathematics.divide(x, y); };
return result;
});
},
_calc_: function (x, y, result, cb) { return cb(x, y, result); },
calc: function calculator(operation, x , y) {
const result = operation(x, y);
return (() => {
return {
value: result,
calc: (operation, y) => {
return calculator(operation, result, y);
}
}
})();
}
};
console.log(Mathematics.calc(Mathematics.product, 4, 2).value);
console.log(Mathematics.calc(Mathematics.product, 4, 2).calc(Mathematics.square).calc(Mathematics.sum, 3).calc(Mathematics.power, 3).value);