Define the value of the variable as an equation

0

I wanted an equation to appear as a function of x and not the value of the equation, is it possible? I did not find anything, and I'm now starting to program in javascript and I do not know much

    
asked by anonymous 27.06.2015 / 18:55

1 answer

1

If I understand you, you want to save a mathematical equation and run it only when it's interesting?

A simple example would be to use eval(); :

var x = "1 + 2 * 3";
var y = eval(x);

console.log(x, "=", y); // Exibe no console 1 + 2 * 3 = 9

Of course many will talk, eval is bad and you can actually do anything in eval, which can be a problem, in this case you can use libraries for this:

  • link - example:

    math.eval('sqrt(3^2 + 4^2)');           // 5
    math.eval('sqrt(-4)');                  // 2i
    math.eval('2 inch to cm');              // 5.08 cm
    math.eval('cos(45 deg)');               // 0.7071067811865476
    
  • link - example:

    Parser.parse("2 * x + 1");
    

    Commands:

    • sen(x) - sine of x (x is in radians)
    • cos(x) - cosine of x (x is in radians)
    • tan(x) - Tangent of x (x is ... well, you know)
    • asin(x) - sine arc of x (in radians)
    • acos(x) - cosine arc of x (in radians)
    • atan(x) - Tangent arc of x (in radians)
    • sinh(x) - Hyperbolic sine of x (x is in radians)
    • cosh(x) - hyperbolic cosine of x (x is in radians)
    • tanh(x) - hyperbolic tangent of x (x is ... well, you know)
    • asinh(x) - hyperbolic arc-sine of x (in radians)
    • acosh(x) - hyperbolic cosine arc of x (in radians)
    • atanh(x) - hyperbolic tangent arc of x (in radians)
    • sqrt(x) - Square root of x. The result is NaN (Not a Number) if x is negative.
    • log(x) - Natural logarithm of x (not base-10).
    • abs(x) - Absolute value (magnitude) of x
    • ceil(x) - Ceiling of x - the smallest integer that is > = x.
    • floor(x) - Floor of x - the largest integer that is
27.06.2015 / 19:18