1.0.1 • Published 1 year ago
@virtuoid/calculator v1.0.1
Fun With JavaScript - 'Calculator' static class
This repository is for the 'Calculator' static class used in the Fun with JavaScript series (https://funwithjavascript.com).
You pass a string equation to the single method, solve()
, and the calulator will return the value of that equation.
This code DOES NOT use eavl()
, but instead a double-stack system.
Versions
1.0.1 : Hotfix - type=module for package.json 1.0.0 : Initial Version
Installation
npm install --save @virtuoid/calculator
Dependencies
No dependencies
Usage
It is important to note that the Calculator class is a static class. It cannot be instantiated. In fact, it will throw an error if you do!
import Calculator from '@virtuoid/calculator';
// Sample equations
let result;
result = Calculator.solve('3+4'); // result = 7
result = Calculator.solve('3-4'); // result = -1
result = Calculator.solve('2*3+4'); // result = 10 (operator precedence)
result = Calculator.solve('2*(3+4)'); // result = 14 (parenthesis done first)
result = Calculator.solve('2*(3+(4*5)-(6+7))*8+9');
console.log(result); // output 169 (stacked parenthesis)
Methods
Name | Returns | Description |
---|---|---|
solve(equation: string) | Number | Evaluates the equation. And error is retturned if the equation is mal-formed. |
Operands
Any valid integer or floating-point number. Scientific Notation, Infinity, and imaginary numbers are not supported.
Operators
Addition (+), Subtraction (-), Multiplication (*), and Division (/).
Groupings
Parenthesis are supported.
Github
https://github.com/TheVirtuoid/fwjs-calculator