skip to Main Content

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


  1. For debugging purposes, you can simply create a function that replaces variable names in the formula with their values. Something like:

    function get_var_string(formula, variables) {
      const varNames = Object.keys(variables);
      let result = formula;
    
      for (const varName of varNames) {
        const varVal = variables[varName];
        const regex = new RegExp('\b' + varName + '\b', 'g');
                              //   ^^^ (word boundary)
    // to ensure that we are matching the exact variable name and 
    // not a substring of another variable name or word.
        result = result.replace(regex, varVal);
      }
    
      return result;
    }
    

    Edit:

    I tried replicating your original scenario maybe if this helps.

    function resolveVariable(path, variables) {
      return path.split('.').reduce((obj, key) => obj[key], variables);
    }
    
    function get_var_string(formula, variables) {
      const variablePaths = formula.match(/b[w.]+b/g);
      let result = formula;
    
      for (const path of variablePaths) {
        const variableValue = resolveVariable(path, variables);
        const regex = new RegExp('\b' + path + '\b', 'g');
        result = result.replace(regex, variableValue);
      }
    
      return result;
    }
    
    const item = {
      tiers: {
        main: {
          low: 5,
          high: 2,
        },
      },
      other: {
        value: 4,
      },
    };
    
    const formula = "item.tiers.main.low * item.tiers.main.high - item.other.value";
    const variables = { item };
    
    const variableString = get_var_string(formula, variables);
    console.log(variableString);
    Login or Signup to reply.
  2. 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

    const a = 5;
    const b = 2;
    const c = 4;
    const d = 6;
    const e = 3;
    
    const formula = "a * b - c * (d / e)";
    const result = formula.replace(/[a-z]/g, m => eval(m))
    console.log(result)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search