I want to transform the example string "0.45*100"
in a value (45
).
Another example, if I have two variables totalCost
and contractVolume
, and I have the string "totalCost * 1000 / contractVolume"
: how can I transform the string in the resulting value?
ex.
JavaScript
x
5
1
var totalCost = 10
2
var contractVolume = 100
3
var stringToCompute = "totalCost * 1000 / contractVolume"
4
console.log(compute(stringToCompute)) // -> 100
5
Obs: I see a similar question, but in php, in the example he uses the method eval()
Advertisement
Answer
you can also use eval() function in javascript
like —
JavaScript
1
2
1
eval("x + 17")
2