0

I'm writing a program that needs to evaluate a large number of different boolean expressions that are determined at runtime by information I read in from my datasource at runtime.

Is it possible to construct such a function in javascript?

My potential workarounds are to construct a string of boolean expressions that could be converted into a working boolean expression. The names of the variables are predetermined so this could be feasible if I knew how to do this, but I don't. Another option would be to run this dynamic boolean expression generating process concurrently with the function I use to read in data, but this would greatly increase the complexity of my program, so I would like to avoid this if possible.

Here are some examples:

Again, my variable names are predefined: let's call them numVar1, numVar2, boolVar for clarity.

Each of these dynamic boolean expressions would be composed of and, or, and not operations like this:

numVar1 > 0 && numVar2 < 0
numVar1 > 1 && !(numVar2 < 2)
numVar2 > 2 || (numVar1 < 100 && (boolVar === true))

each function would have these three parameters and one such boolean expression, but the actual expression would need to be generated from a JSON datafile that I parse myself.

mooglin
  • 500
  • 5
  • 17
  • 1
    Generally speaking: *yes, probably.* But you’ll need to provide some concrete examples and details for actual help. – deceze Jan 31 '21 at 07:55
  • "my variable names are predefined" is the first assumption you should challenge because it very likely isn't true. – Ingo Bürk Jan 31 '21 at 08:36
  • [You can make a simple expression parser](https://stackoverflow.com/a/64934466/) but I'm not sure if that's what you're asking for. – VLAZ Jan 31 '21 at 08:39
  • 2
    If you back up a few steps and describe the higher level problem you're trying to solve, there may be other types of solutions such as a table driven approach that doesn't require writing an expression parser or using the risky `eval()`. – jfriend00 Jan 31 '21 at 09:31

0 Answers0