0

How can I test if a mathematical expression is true?

var equation = "1 + 1 = 2";
if (equation === true){
  document.write("1 + 1 = 2");
}else{
  document.write("1 + 1 =/= 2");
}

For obvious reasons, this code ^^^ doesn't work, but I just wanted to know if there was a function that would do this; evaluate a mathematical expression then return its verity as a boolean (true/false).

Jeff B
  • 29,943
  • 7
  • 61
  • 90
user1892304
  • 617
  • 1
  • 6
  • 11
  • I'd suggest `eval()` but you'd also need to change `=` to `==` for that to work properly. – canon Dec 10 '12 at 19:46
  • 2
    You could eval it *(but for the `=` as noted above)*, but what's the actual situation? This feels like an XY Problem. Otherwise, you could manually parse the string. – I Hate Lazy Dec 10 '12 at 19:46
  • @I Hate Lazy: I am in fact working on a sort of equation solver so that I can get the hang of for and while loops. I just would like to know if such a function existed to test equalities like the one I previously stated. – user1892304 Dec 10 '12 at 19:54
  • @user1892304, why not just log values to the console, or use assertions? `console.assert(1 + 1 == 2, 'oh no what has happened to math?')` – zzzzBov Dec 10 '12 at 19:56
  • Have a look at the second answer here: http://stackoverflow.com/questions/2421362/how-to-check-and-run-math-equation-using-javascript . – mccannf Dec 10 '12 at 19:57

7 Answers7

2

One approach is to use eval; but note that it requires JavaScript notation, not an ad-hoc notation:

var equation = "1 + 1 == 2";         // note: `==`, not `=`
if (eval(equation)){
  document.write("1 + 1 = 2");
}else{
  document.write("1 + 1 =/= 2");
}

This approach has the downside of not being very "safe", since the eval'd string can contain any JavaScript code whatsoever. That greatly restricts the set of situations in which it makes sense to use it.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    +1 for mentioning security concerns. `eval()` can be dangerous when feeding user input into it. – Jeff B Dec 10 '12 at 19:55
1

I'd write a method that simply checks if two values add up as expected:

function addsUp(a, b, x) {
 return a + b === x;
}

if (addsUp(1, 1, 2)){
  document.write("1 + 1 = 2");
}else{
  document.write("1 + 1 =/= 2");
}
robertp
  • 3,557
  • 1
  • 20
  • 13
1
var equation = eval("1+1==2");  

More details here: http://www.w3schools.com/jsref/jsref_eval.ASP

Florin D. Preda
  • 1,358
  • 1
  • 11
  • 25
  • -0.25 for w3schools. Seriously, there are much less misleading sites out there. [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval), for example. – cHao Dec 10 '12 at 20:02
1

You should take a look at this thread : Equation (expression) parser with precedence?

But consider that what you're doing is pretty complicated unless you can break it down into some rules.

If it is always in the format of X (+-/*) Y = Z then you can strip out the information using regular expressions or a bunch of targeted string replacements. If it needs to cope with A + B - C = D + Z (variable numbers of variables on either side of equation) then you will need to look into recursion and/or complicated string parsing.

Community
  • 1
  • 1
Martin Lyne
  • 3,157
  • 2
  • 22
  • 28
0

you have to evaluate your expression:

eval("1+1==2")
Julien May
  • 2,011
  • 15
  • 18
0

how about

var equation = "1 + 1 = 2";
equation = equation.replace('=','==');
if (eval(equation)){
  document.write("1 + 1 = 2");
}else{
  document.write("1 + 1 =/= 2");
}

?

sites
  • 21,417
  • 17
  • 87
  • 146
0

if you use userinputs you might have to double the quotes ... otherwise you get an error Invalid left-hand side in assignment

var equation = "1 + 1 == 2";

if (eval(equation)){
  document.write("1 + 1 = 2");
}else{
    alert('s');
  document.write("1 + 1 =/= 2");
}​
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Mik
  • 1,705
  • 1
  • 14
  • 26