How to check and run math expression using Javascript - Stack Overflow

I'm trying to make a Javascript function that will take a mathematical expression and apply it to

I'm trying to make a Javascript function that will take a mathematical expression and apply it to a predefined number, for example:

var myNum = 10;

function EQ(eq){
// code here 
}

For example the input should me something like this:

EQ("*100/10"); //output 100
EQ("+100");    //output will be 110
EQ("-+=1");    //output false

Is there any way to do that? Thanks

I'm trying to make a Javascript function that will take a mathematical expression and apply it to a predefined number, for example:

var myNum = 10;

function EQ(eq){
// code here 
}

For example the input should me something like this:

EQ("*100/10"); //output 100
EQ("+100");    //output will be 110
EQ("-+=1");    //output false

Is there any way to do that? Thanks

Share Improve this question edited Jul 31, 2014 at 7:40 user123444555621 154k27 gold badges117 silver badges126 bronze badges asked Mar 10, 2010 at 23:07 trrrrrrmtrrrrrrm 11.8k25 gold badges87 silver badges131 bronze badges 2
  • What does jQuery have to do with this? – Tin Commented Jul 8, 2010 at 2:33
  • For future readers wondering the same thing as Tin, jQuery was part of the original title and tags. – ZAD-Man Commented Sep 9, 2013 at 20:36
Add a ment  | 

3 Answers 3

Reset to default 5

You could probably work eval() into a simple solution. For instance:

var myNum = 10;

function EQ(eq) { return eval(myNum+eq); }

alert( EQ("*100/10") ); // outputs 100

I'd encourage you to expand upon this by implementing a try-catch and handling exceptions.

Here's a simple expression evaluator:

function evalExpression(text)
{
  var tokens = text.split(" ");

  var output = [];
  var operators = [];

  var reNumber = /^\d+(\.\d+)?$/;
  var reOperator = /^[\/\+\*\-]$/;
  var precedence = { "+": 1, "-": 1, "*": 2, "/": 2 };

  for (var i = 0; i < tokens.length; ++i)
  {
    var t = tokens[i];
    if (reNumber.test(t))
      output.push(Number(t));
    else if (reOperator.test(t))
    {
      while (operators.length && precedence[t] <= precedence[operators[operators.length - 1]])
      {
        output.push(operators.pop());
      }

      operators.push(t);
    }
    else if (t == "(")
      operators.push(t);
    else if (t == ")")
    {
      while (operators.length && operators[operators.length - 1] != "(")
        output.push(operators.pop());

      if (!operators.length) return false;

      operators.pop();    
    }
    else 
      return false;
  }

  while (operators.length)
    output.push(operators.pop());

  var result = [];

  for (i = 0; i < output.length; ++i)
  {
    t = output[i];
    if (reNumber.test(t))
      result.push(t);
    else if (t == "(" || result.length < 2)
      return false;
    else 
    {
      var lhs = result.pop();
      var rhs = result.pop();

      if (t == "+") result.push(lhs + rhs);
      if (t == "-") result.push(lhs - rhs);
      if (t == "*") result.push(lhs * rhs);
      if (t == "/") result.push(lhs / rhs);
    }
  }

  return result.pop();
}

It supports numbers and + - * / ( ). Tokens must be separated by a single space, e.g.: "1 * ( 2 + 3 )"

Anyway, that's the type of code you'd need if you didn't want to use eval.

try this...

function EQ(input) {
  try {
    var ret = Number(eval('(0+(' + input + '))'));
    return isNaN(ret) ? null : ret;
  } catch(err) {
  }
  return null;
}

You can replace the null default with a literal false if you like...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745455977a4628486.html

相关推荐

  • How to check and run math expression using Javascript - Stack Overflow

    I'm trying to make a Javascript function that will take a mathematical expression and apply it to

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信