If a have the following in Java Script (server script):
const a = 5;
const b = 2;
const c = 4;
const d = 6;
const e = 3;
const formula = "a * b - c * (d / e)";
const result = eval(formula); // = 2
I need, for debugging purposes, to have a Java Script function that will return as result a string with the variables values:
const variables = "5 * 2 – 4 * (6 / 3)"
I named the variables a,b,c,d,e for simplicity, but they are more complex vars from JSON structures, like for example a = item.tiers.main.low, and similar for b,c,d,e.
I tried to do this:
const values = " " + a + " * " + b + " – " + c + " * ( " + d + " / " + e + " )";
which gives the expected result,
but there are dozens of formulas to do this with. To call a function like:
const variables = get_var_string(formula);
would be more convenient.
2
Answers
For debugging purposes, you can simply create a function that replaces variable names in the formula with their values. Something like:
Edit:
I tried replicating your original scenario maybe if this helps.
I strongly advice anyone that reads it to use something else than eval
But…
You can just replace every letter and use eval on them