Reputation: 700
Hello I have one code that performs an operation and show the result. Is it possible to store half operation in a variable and use it dynamically.?
Example
result = (((Math.pow((1045.34/1000), (1/(18/12)))) -1)*4/3)*100;
I want to store "*4/3" in a variable and apply them like
var oper = "*4/3"
result = (((Math.pow((1045.34/1000), (1/(18/12)))) -1)oper)*100;
ANy idea How to achieve this ?
Upvotes: 0
Views: 400
Reputation: 3284
Using the eval
function.
result = eval('(((Math.pow((1045.34/1000), (1/(18/12)) -1)' + oper + ')*100;');
Be careful though, eval
can be dangerous. Check here if you want to know why.
Upvotes: 0
Reputation: 128786
Use a function?
function doStuff(stuff) {
return stuff * 4 / 3;
}
var result = doStuff(((Math.pow((1045.34/1000), (1/(18/12)))) -1)) * 100;
Here we pass ((Math.pow((1045.34/1000), (1/(18/12)))) -1)
into our doStuff()
function, and that returns the result of that multiplied by 4 and divided by 3. We can then multiply it by 100 (as your question does).
result
ends up with the value:
-> 4.000365122980623
Upvotes: 2