A simple, small, and alternative code to replace javascript native eval()
function.
Accept mathematical expression (numerical expressions) based on a string only.
It currently accepts:
- Addition
- Subtraction
- Multiplication
- Division
- Exponentation
- Download the release file.
- Include
src/calc.js
by adding it as a script in yourHTML
file. - Use it!
Eval(" 1 + 1"); //returns 2
Eval("10 - 5"); //returns 5
Eval("6 * 2"); //returns 12
Eval("14 / 7"); //returns 2
Eval("2 ^ 2"); //returns 4
The Eval()
function accepts a string
- It is then parsed by the parser,
- then it will be arrange by the transformer,
- and lastly it will be generated,
- and the expression is evaluated.
- Returns the output as a
string
.
The Eval()
uses the Shunting Yard Algorithm and Reverse Polish Notation.