OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Writing a function that "solves" an equation

  • Thread starter Thread starter replax
  • Start date Start date
R

replax

Guest
I want to write a function which will allow me to "solve" an equation in js.

what I want (not in a programming language):

Code:
function f(x) { 1 + x * x }
var z = 2
var y = f(z)  //y will be 5 as a number

what I have written in JS:

Code:
function P(cfg) { ....
this.equation = "1 + x";
....};
P.prototype.eqn = function(x) {
    var tmp = eval(this.equation);
    return tmp;
};
....
P.prototype.draw = function() {....
for(var i = 0; i < z; i++)
    ctx.lineTo(i, this.eqn(i));
....};

also I've read that using eval in a loop is probably not a good idea, but I have not figured out another way (yet) (JS beginner)...

The problem with this code is, that at least in FF the var tmp will STILL contain the string from this.equation instead of the calculated value.

I would appreciate any further insight very much!

Thank you for your time :)

EDIT: because my question was not formulated very well: after the execution of line var tmp = eval(this.equation); the var tmp will hold a STRING which equals the string this.equation, instead of the desired solution y value. Also I do not mean solve but evaluate, thanks for that tip :)

<p>I want to write a function which will allow me to "solve" an equation in js.</p>

<p>what I want (not in a programming language): </p>

<pre><code>function f(x) { 1 + x * x }
var z = 2
var y = f(z) //y will be 5 as a number
</code></pre>

<p>what I have written in JS:</p>

<pre><code>function P(cfg) { ....
this.equation = "1 + x";
....};
P.prototype.eqn = function(x) {
var tmp = eval(this.equation);
return tmp;
};
....
P.prototype.draw = function() {....
for(var i = 0; i < z; i++)
ctx.lineTo(i, this.eqn(i));
....};
</code></pre>

<p>also I've read that using eval in a loop is probably not a good idea, but I have not figured out another way (yet) (JS beginner)...</p>

<p>The problem with this code is, that at least in FF the var tmp will STILL contain the string from this.equation instead of the calculated value.</p>

<p>I would appreciate any further insight very much!</p>

<p>Thank you for your time :)</p>

<p>EDIT: because my question was not formulated very well:
after the execution of line
var tmp = eval(this.equation);
the var tmp will hold a STRING which equals the string this.equation, instead of the desired solution y value.
Also I do not mean solve but evaluate, thanks for that tip :)</p>
 
Top