Forum Moderators: open

Message Too Old, No Replies

using string characters as operators

taking text field user data to make a calculation

         

imakwik1

5:46 am on Feb 23, 2005 (gmt 0)

10+ Year Member



hi, I am making a calculator and I have everything done except for one small problem. I have one variable for the first number in the calculation, one variable for the second number in the calculation, and one variable for the operator. What I want to do is something like:

answer = variable1 n variable2; (where n is the operator)

is this possible?

thanks,
mark

PS right now I have it set up using switch but it is a real pain... it seems like there is an easy solution to my problem!

Bernard Marx

8:33 am on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Calculators are one of the few legitimate uses of eval:

var a = 2, b = 3, op = '+'; [color=brown]// or a = '2', b = '3'[/color]
answer = eval(a+op+b)

imakwik1

12:46 am on Feb 24, 2005 (gmt 0)

10+ Year Member



thank you thank you thank you!

you would not believe the amount of rediculous suggestions I got for this problem, I knew what I wanted had to exist!

mark

if you want to see it in progress it is at
www.pitt.edu/~msr12/IS/calc.html