1

How can I scope variables to have expressions with other variables in the same scope? I tried this but it doesn't work. I want c to equal b + 2 then I want to be able to get the result of any variable in the scope.

let scope = {
    a: '= b + c',
    b: 1,
    c: 2
};

console.log(math.evaluate('a'), scope);
coder
  • 11
  • 2

1 Answers1

0

You can not evaluate c = 'b+2' inside scope you need to explicitly evaluate c scope

// provide a scope
let scope = {
    a: 3,
    b: 4
}

math.evaluate('c = b + 2', scope)   // 6
scope.c                             // 6
Sourav Singh
  • 878
  • 1
  • 8
  • 14
  • is there another way to do it rather than assigning then evaluating for every variable that is an expression? – coder Jan 03 '20 at 05:17