I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
Asked
Active
Viewed 109 times
0
-
try `eval()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval – tim Dec 06 '19 at 17:29
-
1@tim please no. `Eval is Evil` – Seblor Dec 06 '19 at 17:30
-
`eval` is not always evil... it depends on the 'generator', if the generator is user-driven then yes, it is very evil – GrafiCode Dec 06 '19 at 17:31
-
@GrafiCode agree that it depends on the context. https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea There are some good discussions in the answers of this question – tim Dec 06 '19 at 17:33
-
You can probably avoid the need to parse strings into an executable instruction, but without more code it's hard to help. – BobtheMagicMoose Dec 06 '19 at 17:33
-
maybe this helps a bit: https://stackoverflow.com/questions/37193706/turning-an-array-of-values-and-strings-into-an-if-statement-in-javascript – Nina Scholz Dec 06 '19 at 17:51
1 Answers
0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!

BobtheMagicMoose
- 2,246
- 3
- 19
- 27