An expression calculator for JavaScript
- Basic math operators:
+
- Addition-
- Subtraction*
- Multltiplication/
- Division^
- Power (right associative)%
- Modulo division
- Brackets support
- Operators precedence support
- Custom functions support
- Function recursion support
- Variables support
- Unary operators support
You can try it on JSFiddle
Just call a function:
Compute(expression, functions, variables);
let result = Compute('5 * (3 + 4) + 3');
console.log(result); // displays 38
let result = Compute('10 + dist(5, dist(3, 4))', {
dist: function (args) {
return (args[0] ** 2) + (args[1] ** 2);
}
});
console.log(result); // displays 660
let result = Compute('myvar ^ 2', {}, {
myvar: 32
});
console.log(result); // displays 1024
This project licensed with MIT license
Made with ❤️