1

I am trying to create a simple graphing calculator where a user enters a function of f (like f(x) = x^2+2x+6). Basically the javascript replaces the x in the function with some number and then evaluates the function using eval(). The problem is, I want users to be able to type x^2 instead of default javascript which would be Math.pow(x,2). I'm guessing it's going to be some regular expression but I have little experience with them and find them really confusing, personally. Is it possible to convert a statement like x^3-x^2 to Math.pow(x,3)-Math.pow(x,2) ??

Help greatly appreciated.

croar
  • 33
  • 9
  • 2
    Yes it is possible. Can you show us what you tried so far? You can start with regex here http://www.regular-expressions.info/ – elclanrs Feb 27 '14 at 00:23
  • `eval` is bad practice and is unnecessary for the task (not saying that helps or anything, it's just not a good thing to do in most cases) – user70585 Feb 27 '14 at 00:23
  • `^` isn't your only problem, you have to add a multiplier operator as well (*). – Eric H. Feb 27 '14 at 00:25
  • 1
    This is pretty much the most popular assignment in computer science, it's well covered on the site. Using `eval` and regex is cheating, however. Google ["parse math expression in JavaScript"](https://www.google.ca/search?q=parse+math+expression+in+javascript&oq=parse+math+expression+in+javascript&aqs=chrome..69i57.2856j0j4&sourceid=chrome&espv=210&es_sm=91&ie=UTF-8) and you get thousands of tutorials. – user229044 Feb 27 '14 at 00:26
  • Should the user be able to type `f(x) = x^(2x + 1) + 1/(2^(2*x))`? – Pointy Feb 27 '14 at 00:27

2 Answers2

4

You want to use a Regular Expression that looks something along the lines of

(.+)\^(.+)

This will match both selections, you then replace the instances of that string, using those matches like this.

Math.pow($1, $2)

Javascript has support for this kind of operation with the function option in String.prototype.replace

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
0

1) Yes, it is possible. You can easily program it yourself if you parse what the user entered. Wherever you see x^n just turn it into Math.pow(x,n). And only then eval. This will work for say polynomials of one variable.

2) If you want to solve this more generally (for a broader class of math functions), you need to come up with some grammar and build an AST from the user input.

http://en.wikipedia.org/wiki/Abstract_syntax_tree

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I didn't (and won't) downvote but your answer really doesn't *explain* much. (The question is a little broad, of course.) – Pointy Feb 27 '14 at 00:25
  • @peter.petrov You really don't give a useful answer to the question. If I asked this question, then I would expect an answer that doesn't restate what I gave as an example when I asked. – Blue Ice Feb 27 '14 at 00:26
  • With the edit, it provides new info. I'm removing my downvote. :) – Blue Ice Feb 27 '14 at 00:28
  • I think the asker wants to know how to turn x^n into the aforementioned, you've just told him to do it. – Dan Prince Feb 27 '14 at 00:30
  • @DanPrince OK, maybe. I just understood this question slightly differently. – peter.petrov Feb 27 '14 at 00:31