0

Lets say I have the following variables (they're all strings):

x = "4"
op1 = "+"
y = "5"
op2 = "-"
z = "3"

How would I take these strings and make an equation that would give me the answer 6?

Snubber
  • 986
  • 1
  • 10
  • 24

3 Answers3

1

One quick way you could do it in is using evil eval, similar to this:

var result = eval(x + op1 + y + op2 + z);

That will populate result with 6.


DEMO - Using evil eval


eval() is well suited for evaluating equations in string format. As eval() executes anything within the string passed you should look at validating/clean the data before before running eval() on it, specially in a public facing application.

Have a look at this SO post for an example on how to check the string is safe, in the context of equations, before using eval on it, assuming this it is something you have to worry about in your app.


Alternatives to eval()


While using eval() correctly and well managed is not the worst thing a non-eval() alternative would be great.

If you look at the comments on Shryme's own answer, there is an interesting way outlined. the interesting part of the comment is myOperator.execute(number1, number2);.


Why is eval evil?


eval() is not that special and is dangerous just or the same reason any other advanced language feature is dangerous when not understood and miss-used.

eval() is the most commonly used means of evaluating code at runtime.
Note that it executes code in a string in the scope within which it was called. It will blindly execute any malicious code!

eval() returns the result of the last evaluated expression. Hence if you execute several expressions only the result of the last one will be returned.

eval() is possibly the most miss-used short-cut and the cause of a lot of hours of debugging when it comes to scoping issues or results are not as expected when miss-used.

Community
  • 1
  • 1
Nope
  • 22,147
  • 7
  • 47
  • 72
0

Well, 4 + 5 - 3 would give you the answer 6. To use the strings into an equation, I believe you could do x + op1 + y + op2 + z? Make sure it is of type string

user2925924
  • 45
  • 2
  • 12
0

You can make a function that take 2 number and 1 operator, make a switch inside with the operator, do the math then return the result, then you recall the function with the result, your third number and the second operator and you have your answer

Shryme
  • 1,572
  • 1
  • 14
  • 22
  • Im not sure this would be very dynamic. If you only had a single operator than sure but if you can have multiple operators you will end up with quite a nested n-th long process. – Nope Nov 26 '13 at 09:10
  • My answer was to help him understand one way to do it without eval(), i know it's not the best nor the fastest way, but he can start from there and make it better after. Personally I did a text calculator with graphics for formula and i parsed the string and determine numbers and operators, I created a class operator and implemented priority and all math were done in this class, all i was doing is calling myOperator.execute(number1, number2); But i found this answer overkill for it's need, that's why i gave a simpler one! – Shryme Nov 26 '13 at 13:14
  • 1
    Don't get me wrong I didn't think your answer was wrong nor did I mean to question the validity of it. I merely was wondering about the possible complexity given the number of operators and operator chaining as I never done this before myself either. Your explanation in your comment seems to add some interesting ways of doing it though. Thanks for the reply. – Nope Nov 26 '13 at 14:08