-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalMath.js
More file actions
50 lines (40 loc) · 1.37 KB
/
evalMath.js
File metadata and controls
50 lines (40 loc) · 1.37 KB
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
// Simple math eval function (also known as "calculator")
const functions = {
sqrt: num => Math.sqrt(num),
cbrt: num => Math.cbrt(num)
};
const operators = {
"+": (a, b) => a + b,
"-": (a, b) => a - b,
"*": (a, b) => a * b,
"/": (a, b) => a / b
}
const reg = (/(\d+)\s*([*/])\s*(\d+)/g);
function evalMath(str) {
str = str.replace(/([a-z]*)\((.*?)\)/gi, (_, f, n) => {
const num = evalMath(n);
return functions[f] ? functions[f](num) : num;
});
while (str.match(reg)) {
str = str.replace(reg, (_, a, o, b) => operators[o](parseFloat(a), parseFloat(b)));
}
let num = 0;
let lastOpt = "+";
const arr = str.split(/(\d+|[+-])/g).map(x => x.trim()).filter(Boolean)
for (const s of arr) {
if (operators[s]) {
lastOpt = lastOpt === s ? "+" : s;
} else {
if (operators[lastOpt]) num = operators[lastOpt](num, parseFloat(s));
lastOpt = "";
}
}
return num;
}
console.log(evalMath("123 + 321")); // 444
console.log(evalMath("30 + (-6)")); // 24
console.log(evalMath("6 / 3 + 3")); // 5
console.log(evalMath("sqrt(16) + 3")); // 7
console.log(evalMath("sqrt(64) / cbrt(8) * 3")); // 12
// Bugs
console.log(evalMath("cbrt(sqrt(16) + 4)")); // NaN Because the regex for brackets locate the nearest bracket closing, so the match will be: "cbrt(sqrt(16)"