-1

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.

var totalCost = 10
var contractVolume = 100
var stringToCompute = "totalCost * 1000 / contractVolume"
console.log(compute(stringToCompute)) // -> 100 

Obs: I see a similar question, but in php, in the example he uses the method eval()

Url

Khaos101
  • 442
  • 5
  • 14
Matheus
  • 52
  • 1
  • 11

2 Answers2

1

you can also use eval() function in javascript

like --

eval("x + 17")
0

You can use eval() in JS too. Usually it is not a good idea if it is not mandatory, but it works.

Khaos101
  • 442
  • 5
  • 14