0

I am blacking out over issue and I am convinced I am thinking too complex about this, but summarized, my issue is about this:

// imagine this variable is dynamically filled by a loop with conditions inside
var condition = varA + " == " + varB + " && " + varC + " == " + varD;

if (condition) {
    // something
}

So it doesn't check whether varA equals varB and varC equals varD as what I intended, but instead it just sees it as a string and that appears to be always true anyway. I don't want, I want to see it actually checking whether varA equals varB etc.

Is there a way to parse this statement into something that actually can be a 'legit' if condition?

Thanks!

6
  • 1
    you could use eval() but it's generally frowned upon. still why not just write that if statement? it doesn't really make sense to build it this way Commented Nov 28, 2017 at 20:33
  • 2
    Have you tried looping through two variables at a time? runningBool = runningBool && varX == varY. At the end of the loop, runningBool will be true or false. Commented Nov 28, 2017 at 20:36
  • @RobbieMilejczak I get what you think, as if (varA == varB && ...) would normally work as well indeed, but that won't help in my situation as in this if-statement the amount of conditions varies per case. It could be that the condition varA == varB is omitted dynamically through filtering so only varC == carD remains. Case is too that every (dynamically added) condition needs to be true. I wonder if this is the way to go to accomplish this anyway, so it's the question how else I would be able to do this. Commented Nov 28, 2017 at 20:38
  • like I said you can use eval() but why not just use a switch statement and handle the amount of variables with separate cases? Commented Nov 28, 2017 at 20:40
  • @RobbieMilejczak I think I thought about the same as what you wrote, with the switch-statements, but what if the amount of conditions is more than 3? In that case I'd have to make a lot of hard coded different cases for each possible condition, as A==B && C==D && E==F could also be A==B && E==F and so on. Do I misinterpret you here? Commented Nov 28, 2017 at 20:47

3 Answers 3

1

I think I understand what your trying to do. You are trying to look at a number of comparisons and determine, in the end, if all comparisons were truly true.

In this case you can actually just keep building your condition out as in the following:

var a = 1, b = 1, c = 3, d = 3, e = 5, f = 6; 
var condition = a === b; // condition is true
condition = condition && c === d; // condition is still true
condition = condition && e === f; // condition is now and forever false
Sign up to request clarification or add additional context in comments.

Comments

1

why not just do

if(varA  ==  varB  &&  varC ==varD){
//do something
}

edit

maybe try using safe-eval. its a third party package but APPEARS to be an improvement on eval.

1 Comment

The comment in his/her code at the top suggests that the condition is dynamically populated. If it were just varA == varB && varC == varD this would be a non-issue.
0

The issue with your condition variable is that it's not checking for equality between your variables. The whole condition variable is being read as a string because it sees the == and && as strings and ends up concatenating them together.

I suggest writing your if statement like so:

if (varA === varB && varB === varC && varC === varD) {
  // do something
}

That way, you're checking if varA is equal to varB and varB is equal to varC, etc. Using the triple equals also ensures that the variables are of the type value type (i.e. string, boolean, number). Hope this helps.

1 Comment

I know about the existance of ===, thanks for that and right you are, but that is not the general issue here. The issue is about the dynamic condition, where varA could suddenly not check varB if it would be equal (while it could with the rest).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.