0

I'm working on a personal project right now ( I'm trying to plot a graph with the given expression in javascript), and I'm facing an issue.

Say s = "3x^2+4x+5", I'm trying to convert this into return 3*x*x + 4*x + 5, in the function

myGraph.drawEquation(function(x) {
    return 3*x*x + 4*x + 5;
}, 'green', 3);

I've looked around and I came across eval(), but it needs to substitute a value into x. Other guides say that I need to make my own parser, but how do you create a parser that does the above?

Currently I have a function to convert s = "3x^2+4x+5" into ["+3x^2","+4x","+5"] into ["+3*x*x","+4*x","+5"], if that helps.

(Sorry for the multiple edits)

Tan WS
  • 181
  • 1
  • 2
  • 12
  • Are you always dealing with polynomials of a similar type? – recursive Feb 20 '15 at 02:49
  • Yes, once I handle this I'm hoping to handle cubic and trigo functions – Tan WS Feb 20 '15 at 02:57
  • 1
    To do anything of reasonable complexity you'll need to write/use some kind of parser. – Evan Trimboli Feb 20 '15 at 03:06
  • possible duplicate of [Evaluating a string as a mathematical expression in JavaScript](http://stackoverflow.com/questions/2276021/evaluating-a-string-as-a-mathematical-expression-in-javascript) –  Feb 20 '15 at 03:23

3 Answers3

1

if you have this ["+3*x*x","+4*x","+5"]

Than you can do something like below to evaluate.

// Code goes here

function myFunction(){
  //alert('loaded')
  var myexpr = ["+3*x*x","+4*x","+5"];
  var expr = "";
  for(s in myexpr)
  {
    expr += myexpr[s];
  }
  expr = "y = "+expr
  setH1Text(expr);
  
  myEval(expr,"x = 1");
}

function setH1Text(text)
{
  document.getElementById('jenish').innerHTML = text;
}

function myEval( ex,  ex1)
{
  var evaltobeexecuted  = ex1+";"+ex
  
  eval(evaltobeexecuted)
  setH1Text(y);
  //alert(y)
}
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body onload="myFunction()">
    <h1 id="jenish">Hello Plunker!</h1>
  </body>

</html>

Note : you need to place some substitution that x = 2 passing to second expression in myEval function. ultimately you can also build like I have built main expression expr.

Caution : Use of Eval is evil. It casues security issues like cross site scripting.

You can also make use of Math.js (I highly recommend this to you just use that have already build if possible :))

function myFunc()
{
  var scope = {
  x: 2
  };
    
    var f = math.eval('f(x) = 3x^2+4x+5', scope);
    alert(f(2));
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/1.3.0/math.js"></script>
<body onload="myFunc()">
  
</body>
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
1

You could use the expression parser of math.js for this, it supports defining functions:

var f = math.eval('f(x) = 3x^2+4x+5');
console.log(f(1)); // outputs 12
console.log(f(2)); // outputs 25
console.log(f(3)); // outputs 44

math.js has it's own expression parser, it does not use JavaScript's eval function.

Jos de Jong
  • 6,602
  • 3
  • 38
  • 58
0

You're going to need some regular expression education. This still won't solve for x, but it's worth looking at:

function youreWelcome(str, x){
  return str.replace(new RegExp('('+x+')', 'g'), '*$1').replace(new RegExp('('+x+')\\^(\\d)+', 'g'), function(m, p1, p2){
    var r = [];
    for(var i=0; i<p2; i++){
      r.push(p1);
    }
    return r.join('*');
  });
}
console.log(youreWelcome('3x^2+4x+5', 'x'));
console.log(youreWelcome('7n^3+42n^5+8', 'n'));

Look into Math.pow() when you're trying to solve for x.

Check this out:

JavaScript: Calculate the nth root of a number

Community
  • 1
  • 1
StackSlave
  • 10,613
  • 2
  • 18
  • 35