title: Writing a function that "solves" an equation
body: <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 &lt; 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>