+ 5
question about Javascript?
I created a function and I want to behavior with that function's element like javascript code. you will understand my meaning with the following example: function a(elem){ } a("2+2") answer=4 a("5^2") answer=25 a("4+2+9") answer=15 a("6*5+4") answer=34 Can you help me?
2 Réponses
+ 4
You're passing in a string expression to be evaluated, so you could just use the eval() function by itself or return eval(elem) which will return the evaluation from your function.
function a(elem) {
return eval(elem);
}
x = a("2 + 2");
alert(x);
or just:
x = eval("2 + 2");
alert(x);
+ 3
thanks a lot