Reputation: 1765
I am writing a code that can validate if a condition is correct or not. A condition is an expression which results in true
or false
.
for example: X+Y>Z
is a valid condition. X+Y
is not.
And then, there can be complex conditions like ( X + Y > Z ) && ( X - Y < Z )
which is also correct. But ( X + Y > Z) && Z
is not. These conditions can be as complex as then need to be. I used javascript eval() function to validate those conditions. Here is my code:
try{
var evaled=eval(expression);
if(evaled===true||evaled===false) alert("Valid.");
else alert("Invalid");
}catch(err){
// eval throws exception on syntax error
alert("Invalid. Syntax error.");
}
This works well for most of things but results valid in cases like this : ( X > Y) && Z
which should be invalidated. Also, expression like this X + Z && (X>Z)
is also showing valid. How can I make this validation correct.
Upvotes: 0
Views: 74
Reputation: 1765
I finally found a solution to my question. I created a way of signaturing all the conditions.
For all the operands in the condition, I placed x
in the signature,
for all arithmetic operators, i placed an o
,
for logical operators l
,
for relational operators t
,
for not operator n
.
Then I kept replacing xox
with x
until there are no arithmetic operators, then I replaced all xtx
with c
, c
is for condition. then I replaced nc
and clc
with c
.
In the end, if the signature results in a single c
, its a valid condition, else its not.
here is my code :
while(exp.indexOf("xox")!=-1||exp.indexOf("(x)")!=-1){
exp=exp.replace("xox","x");
exp=exp.replace("(x)", "x");
}
while(exp.indexOf("xtx")!=-1||exp.indexOf("(c)")!=-1){
exp=exp.replace("xtx","c");
exp=exp.replace("(c)", "c");
}
while(exp.indexOf("clc")!=-1||exp.indexOf("(c)")!=-1||exp.indexOf("nc")!=-1){
exp=exp.replace("clc","c");
exp=exp.replace("(c)", "c");
exp=exp.replace("nc", "c");
}
if(exp==c){
return true;
}else return false;
Upvotes: 0
Reputation: 701
To continue Amadan thoughts, in your conditions if Z is defined so it will return true
and if X are higher then Y so ( X > Y ) && Z
goona be true and X + Z
gonna be true if X and Z are defined values.
You can try Boolean(your expression)
to see what it gonna be.
Upvotes: 0
Reputation: 13113
Try constructor === Boolean
console.log(eval("1+2").constructor === Boolean); //false
console.log(eval("1+2 > 1").constructor === Boolean); //true
console.log(eval("true").constructor === Boolean); //true
console.log(eval("false").constructor === Boolean); //true
Upvotes: 1
Reputation: 198476
eval
will give you the result according to currently defined variables, and according to JavaScript boolean rules, in which all values are either truthy and falsy, and thus permissible arguments for &&
, ||
and !
. I.e. true || ("foo" + 1)
is a valid expression in JavaScript, and will evaluate to true
- as will "moo" && X < 3
if X
is 1
.
If you want to check things according to your own grammar, you can't use eval
, and you will have to implement your own parser (possibly using a parser generator such as Jison)
EDIT: thanks for sharp eyes, Nateowami.
Upvotes: 1