Simply, Evaulate Formulas in JavaScript
npm install @koodu-platform/formula
-
Create a new instance of the
Formula
class by passing the polynomial formula as a string and an optional array of variables:const formula = new Formula('3x^2 + 2x - 1', ['x']);
The formula should be a valid polynomial expression in the form of
ax^n + bx^(n-1) + ... + cx + d
, wherea
,b
,c
,d
, etc. are coefficients,x
is the variable, andn
is the degree of the polynomial. -
Call the
call
method on theFormula
instance, passing the values of the variables as arguments:const result = formula(2);
The
call
method evaluates the polynomial formula with the provided variable values and returns the result.Note: The number of arguments passed to the
call
method must match the number of variables specified when creating theFormula
instance. Otherwise, an error will be thrown. -
Handle the result returned by the
call
method:console.log(result); // Output: 55
The result is the evaluated value of the polynomial formula with the given variable values.
see LICENSE for more details.