-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-7.js
49 lines (40 loc) · 1.27 KB
/
day-7.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
// https://dev.to/thepracticaldev/daily-challenge-10-calculator-23n7
// @author Mohamed Elidrissi
const test = require('./test');
// Ikr, why dont I just use javascript's `eval` function?
// Well, it ain't a challenge unless you make it challenging
function calculate(exp) {
if (exp === undefined || exp === '') return null;
const divisions = exp.match(/-?[0-9]+\/[0-9]+/g);
if (divisions !== null) {
divisions.forEach(v => {
exp = exp.replace(v, eval(v));
});
}
const multiplications = exp.match(/-?[0-9]+\*[0-9]+/g);
if (multiplications !== null) {
multiplications.forEach(v => {
exp = exp.replace(v, eval(v));
});
}
const substractions = exp.match(/-?[0-9]+\-[0-9]+/g);
if (substractions !== null) {
substractions.forEach(v => {
exp = exp.replace(v, eval(v));
});
}
const additions = exp.match(/-?[0-9]+\+[0-9]+/g);
if (additions !== null) {
additions.forEach(v => {
exp = exp.replace(v, eval(v));
});
}
return eval(exp);
}
// FIXME: Multiple operations with the same operator sometime break the calculator. e.g:
// test(10, calculate('2+2*2*2'));
test(36, calculate('10+2*14-4/2'));
test(28, calculate('18/2-15+34'));
test(46, calculate('8+23+4*5-10/2'));
test(null, calculate(''));
test(null, calculate());