2

Any basic addition, subtraction, multiplication and division equation can be entered in the console or set to a variable:

var solve = (3 + 7) + 2 * 8 / 2 // 18

and correctly solved.

My code is not doing this. I believe it has something to do with the textarea. When I enter (3 + 7) + 2 * 8 / 2 it displays (3 + 7) + 2 * 8 / 2 (instead of 18). Note that my code doesn't have a solve button, it solves as the problem is entered.

Fiddle: http://jsfiddle.net/kt4SL/

HTML

<textarea cols="50" rows="10" id="pSolve"></textarea>
<div id="answer"></div>

JavaScript

var input = document.getElementById("pSolve");

input.onkeyup = function() {
    // Solve the input
    finalAnswer = pSolve.value;

    // Display answer
    answer.innerHTML = finalAnswer;
}

I tried this:

// Solve the input
finalAnswer = Number(pSolve.value);

thinking it would fix it but it didn't work. Instead nothing was displayed. What am I missing or doing wrong?

user1822824
  • 2,478
  • 6
  • 41
  • 65
  • 7
    It looks like the easiest thing for you to use is `eval`. It's not necessarily the best solution, though. – Gabe Sep 07 '13 at 22:08
  • 3
    This is a fun little parsing/shunting project to do - but it's already been done alot before too (and I am sure there are duplicates). This is usually given as a programming assignment to undergrads. – user2246674 Sep 07 '13 at 22:14
  • @Gabe `eval` is the perfect solution. Especially when you're the one providing the input. – Bart Sep 07 '13 at 22:33
  • @Bart: `eval` is great if your input language exactly matches JS expressions. If any part of your syntax doesn't perfectly match JS, though, you can't use it. – Gabe Sep 08 '13 at 03:17

4 Answers4

3

Just use eval to evaluate the equation.

answer.innerHTML = eval(finalAnswer);

I updated your jsFiddle


Have a look at this answer as well. The question was about parsing a formula but it's pretty much usable for anything really.

Community
  • 1
  • 1
Bart
  • 17,070
  • 5
  • 61
  • 80
1

You are just extracting a String from the text area and pasting it into the answer div. Strings are not just evaluated by javascript! What you are actually doing is var solve = '(3 + 7) + 2 * 8 / 2';! You would need a parser to actually solve that problem, like in this SO post.

Community
  • 1
  • 1
Janis F
  • 2,637
  • 1
  • 25
  • 36
1

You can do repeated replacement to evaluate arithmetic expressions thus:

function performOp(_, a, op, b) {
  switch (op) {
    case '+': return +a + +b;
    case '-': return +a - +b;
    case '*': return +a * +b;
    case '/': return +a / +b;
  }
}

function evaluateArithmetic(expr) {
  expr = expr.replace(/\s+/g, '');
  while (true) {
    var oldExprPS = expr;
    while (true) {
      var oldExprDM = expr;
      expr = expr.replace(/\((-?\d+)\)/g, '$1');
      expr = expr.replace(/(-?\d+)([*\/])(-?\d+)/g, performOp);
      expr = expr.replace(/\((-?\d+)([+\-])(-?\d+)\)/g, performOp);
      if (expr == oldExprDM) { break; }
    }
    expr = expr.replace(/(-?\d+)([+\-])(-?\d+)/g, performOp);
    if (expr == oldExprPS) { break; }
  }
  return +expr;
}

var input = document.getElementById("pSolve");

input.onkeyup = function() {
  // Solve the input
  finalAnswer = evaluateArithmetic(pSolve.value);

  // Display answer
  answer.innerHTML = finalAnswer;
};
Bart
  • 17,070
  • 5
  • 61
  • 80
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 2
    Does this actually take parentheses into account? – Ry- Sep 07 '13 at 22:14
  • No, it doesn't handle parentheses at all. I still like the solution, it's pretty neat for a very simple version of the problem. – Ingo Bürk Sep 07 '13 at 22:22
  • @minitech, My first cut did not, but I believe it does now. It's basically a simple inside-out parser and tree evaluator so now uses two loops to handle the two levels of operator precedence. – Mike Samuel Sep 07 '13 at 22:42
  • @ Mike Samuel Thanks for this but why wouldn't I just use eval? It seems so much simpler and works fine for what I'm doing. – user1822824 Sep 07 '13 at 22:55
  • @user1822824, `eval` is overkill for this. It's way too powerful a tool, and so opens you up to all kinds of security risks like XSS. – Mike Samuel Sep 07 '13 at 23:58
1

You need to do more than that to solve the equations entered in a text box. Your current script only displays the content of the text box as is. You should use MathJS. It should help.

MathJS Home