Saturday, September 7, 2013

How to convert operator from string type to operator type

How to convert operator from string type to operator type

I want to generate something like this at random.
233 + 333 = 555
I mean the first number and the operator and the second number are
generated at random.
So, I wrote this code now.
var x = parseInt(Math.random()*1000),
y = parseInt(Math.random()*1000),
operators = ['+', '-', '*', '/'],
operatorNum = parseInt(Math.random()*4),
operator = operators[operatorNum],
result;
result = x + operator + y;
But, this just give me something like this "748/264" as string. It doesn't
give me the result of the calculation generated.
I suppose I need to convert operator from string to operator type. But, I
don't think there is a operator type.

No comments:

Post a Comment