-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001_a.js
131 lines (108 loc) · 3.06 KB
/
001_a.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
001: Write a Solver class/object in Javascript that would accept an array in the form of:
[3, “Plus”, 1], [6, “Times”, 2]... or an object with properties “operand1”, “operator” and “operand2”,
the option that you prefer, and returns the result of the arithmetics.
Note: element representing the operator is the name of a function or a reference to that function –
implement them as well (Plus, Minus, Times and Divide).
Note: in the future there may be a need to support another operations without changing the existing
code or changing it as little as possible.
*/
var Solver = function () {
var results = [];
for (var i in arguments) {
results.push(SolveItem(arguments[i]));
}
return results;
};
var SolveItem = function (operation) {
var self = this,
operand1 = operation[0],
operator = operation[1],
operand2 = operation[2];
if (operand1 instanceof Array) {
operand1 = SolveItem(operand1);
} else {
operand1 = parseInt(operand1, 10);
}
if (operand2 instanceof Array) {
operand2 = SolveItem(operand2);
} else {
operand2 = parseInt(operand2, 10);
}
function Plus(a, b) {
return a + b;
}
function Minus(a, b) {
return a + b;
}
function Times(a, b) {
return a * b;
}
function Divide(a, b) {
return a / b;
}
switch (operator) {
case 'Plus':
total = Plus(operand1, operand2);
break;
case 'Minus':
total = Minus(operand1, operand2);
break;
case 'Times':
total = Times(operand1, operand2);
break;
case 'Divide':
total = Divide(operand1, operand2);
break;
default:
//It could have been an alert, an exception, but a console error will be visible during development
console.error('"' + operator + '" is not within the valid operations for this function: ' + typeof self);
break;
}
return total;
};
var SolverLazily = function () {
var results = [];
for (var i in arguments) {
results.push(SolveItemLazily(arguments[i]));
}
return results;
};
var SolveItemLazily = function (operation) {
operation = JSON.stringify(operation);
operation = operation
.replace(/\s/g, '')
.replace(/\[/g, "(")
.replace(/\]/g, ")")
.replace(/,"Plus",/g, '+')
.replace(/,"Minues",/g, '-')
.replace(/,"Times",/g, '*')
.replace(/,"Divide",/g, '/')
return eval(operation);
};
var total = Solver(
[1, "Plus",
[
1,
"Plus", [
2,
"Divide",
2
]
]
], [2, "Plus", 2]);
console.log('Total: ');
console.dir(total);
var totalLazyApproach = SolverLazily(
[1, "Plus",
[
1,
"Plus", [
2,
"Divide",
2
]
]
], [2, "Plus", 2]);
console.log('Total Lazy Approach: ');
console.dir(totalLazyApproach);