0

I need to convert a string returned from prompt into an equation, however the parseFloat returns as only the first number, and symbols in an equation, and stops at the variable. The variable will always = x. The program is designed to convert an algebraic expression say 15*x(5^4-56)*17/x=15 into an expression, and calculate the value of x. If someone could show me how to do this, it would help dramatically. I am currently using multiple prompts, having the user put in the equation before x, then the equation after x, then it inserts a variable in between the two, and calculates it's value.

Edit:

I have no variables predefined, and it must work in equations where x > 1000, or x != //an integer.

Thanks in advance!

Travis
  • 1,274
  • 1
  • 16
  • 33
  • 1
    Did you consider using a regex to parse the string into usable tokens? – PartyLich Dec 11 '14 at 20:56
  • @PartyLich No, I actually didnt How would I do that? – Travis Dec 11 '14 at 21:32
  • Use Mathematica or something. See http://davidwees.com/coding/2006/11/javascript_systems_of_equation.html, http://stackoverflow.com/questions/14191678/solving-linear-equations-similar-algebra-problems-with-javascript, http://stackoverflow.com/questions/4514302/javascript-equation-solver-library. –  Dec 12 '14 at 03:14
  • The example you give has `x` in both numerator and denominator, which cancel out, and means that there is no solution. That's just one case showing what a difficult theoretical problem this is. You shouldn't really try to write this yourself. People have spend entire careers writing such solvers, especially if you want to handle very general cases including polynomials, trigonometric functions, multivariate epxressions, etc. –  Dec 12 '14 at 03:23
  • @torazaburo The program is simply to help people in my math class with algebra.Many of them are struggling, and they simply put in the question, and they get the answer and an explanation. I'm not trying to do stuff like trigonometry, or multivariate expressions. – Travis Dec 12 '14 at 03:26
  • I would recommend a library to parse the expression. You can find these relatively easily on the web. Then you need to write JS which restructures the expression to isolate the variable being solved for on one side, which might be easier, in cases like `x + 1 = 2`, and harder in cases like `x / (x+1) = .9`. –  Dec 12 '14 at 03:29

3 Answers3

1

Seems to be a complex problem...

This is a solution for a simple relaxed version of your problem. Hope you can use some components of this.

Constraints:

  1. answer for x should be integers between 0 and 1000
  2. the left hand side of the expression should be proper javascript syntax

var input = prompt("enter the equation");  //eg: x*x+x+1=241
var parts = input.split('=');

//solving equation starts
var x = 0;
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){
   x++;
   temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this";
//solving equation finishes
  
alert(ans);

You can replace the "solving equation" part with some numerical methods used in computer science to solve equations (more details here) . You will have to parse the left side of equation and map them to proper javascript expressions (as a string to execute with eval()) if you want to allow users to use your syntax.

Sampath Liyanage
  • 4,776
  • 2
  • 28
  • 40
  • Is there any way you could give this a wider range, or a rational number spectrum rather than integer? – Travis Dec 13 '14 at 20:15
  • In this example x is incremented by 1 at each iteration. Instead you can increase it by 0.1 or less.. The proper way to do this is vy using numerical methods in computer science (http://en.wikibooks.org/wiki/Numerical_Methods/Equation_Solving).. – Sampath Liyanage Dec 13 '14 at 20:23
0

Javascript can evaluate strings using the eval function, but the variable as to be defined before hand, and the equation has to be formatted in way that javascript can understand:

var x = 15
var string = "15*x*17/x"
eval(string)

Your example: "15*x(5^4-56)*17/x=15" would not run however, because it would evaluate x(5^4-56) as a javascript expression, which is invalid.

Jan Drewniak
  • 1,316
  • 15
  • 18
0

Using all the info, and other mehtods I found about this, I have put together a communinty answer. Anyone is invited to change and/or add their methods to this.

In order to do this with the least work possible for the user and coder, you would implement the following code.

var input = prompt("enter the equation");  //eg: x*x+x+1=241
var parts = input.split('=');

//solving equation starts
var x = 0; //Or the lowest possible value of "x"
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){ // && x < The highest number to evaluate
   x++; //Add the increment (determines the maximum amount of digits) eg x+0.1 for tenths max, x+2 for only even integers etc.
   temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this"; //make sure x< is the same as line 7.
//solving equation finishes
  
alert(ans);

But, this runs very slowly if you allow tenths, or a range larger than 2000.`

A faster way of running this would be to define arrays allowing any variable (instead of just x) and a different eveulation process such as here. (do the right click view html and click on the first js source to see code) but, this is 2k lines. Both are usable, but the second is more efficient, and can solve multivariate equations.

Travis
  • 1,274
  • 1
  • 16
  • 33