-1

I'm currently making a bot for some reason, i wanted to add a command that will do math when some user typed <command> 1 + 2 and bot will answers 3. And when it's <command> 2 * 3, it will reply 6. (Let's just use textbox for this case, if I typed 1 + 2 in textbox, theres an alert that shows "3").

My question is, is it possible, by any chance to make this kind of command? If so, I'd like to know how. Since I've learned a little about JavaScript RegEx and i think if i use it, it'll work out.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • What have you tried? What research have you done? Certainly this is possible, but a question on SO is expected to show research effort. – random_user_name Dec 08 '17 at 17:55
  • For example, I think your question is actually a complete duplicate of this: https://stackoverflow.com/questions/2276021/evaluating-a-string-as-a-mathematical-expression-in-javascript – random_user_name Dec 08 '17 at 17:56
  • I'm not sure about this, since i'm totally new. That's why i'm here – Brian Harianja Dec 08 '17 at 17:57
  • I'll check it out @cale_b – Brian Harianja Dec 08 '17 at 17:57
  • 1. define operators: `const operators = {add: (a, b) => +a + +b, mult: (a, b) => +a * +b }` - 2. parse with regex: `const op = "mult 1 2".match(/^(\w+)\s+(\d+)\s+(\d+)$/);` - 3. evaluate: `if (op && operators[op[1]]) alert(operators[op[1]](op[2], op[3]));` – le_m Dec 08 '17 at 18:03
  • You could use `eval` after validating that it is only `/[0-9+-*/.]*/` – Jonas Wilms Dec 08 '17 at 18:04
  • Isn't the `command` implicit in the type of math operation? So you wouldn't write `Add 1+2`, just `1+2`. – Andy Dec 08 '17 at 18:07

3 Answers3

1

There's a lot of things you could do. You could eval the input, but this isn't good if you want to let other people use it (since it will execute any valid JavaScript).

The best approach I think, is to make a command for each action. Example:

add 2 2 // 4
sub 2 2 // 0
mul 2 2 // 4
div 2 2 // 1

Here's a function that will run the add command, you can add switch cases for sub, div, and mul.

function run(input) {
    input = input.split(" ");
    switch (input[0]) {
        case "add":
            return parseInt(input[1]) + parseInt(input[2]);
            break;
     }
}

if you don't want to make your bot this way, then it becomes very complicated. You either have to eval the input, but first validate that the input isn't something bad. Or you can build your own expression parser, which is also very hard.

Eli Richardson
  • 934
  • 7
  • 25
1

Here's a quick example that picks up an expression from a text box when the button is clicked, and performs a calculation. It uses a regex to separate out the numbers/operator, and a switch to test the operator cases. Very simple.

const input = document.querySelector('input');
const button = document.querySelector('button');
button.addEventListener('click', handleClick, false);

function handleClick() {
  const txt = input.value;
  const expr = txt.match(/(\d+)\s?([\+\-\*\/])\s?(\d+)/);
  const first = Number(expr[1]);
  const operator = expr[2];
  const second = Number(expr[3]);
  switch (operator) {
    case '+':
      console.log(first + second);
      break;
    case '-':
      console.log(first - second);
      break;
    case '*':
      console.log(first * second);
      break;
    case '/':
      console.log(first / second);
      break;
  }
}
<input/><button>Submit</button>
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 1
    Thanks, i ended up using math.js along with code you've sent to me, maybe i just need to edit a little so it'll work on my project – Brian Harianja Dec 10 '17 at 16:22
-2

You could try to use eval function

Nikola Yankov
  • 1,264
  • 1
  • 15
  • 28
  • 1
    `eval` for a beginner programmer is like evil, too risky and dangerous. -1 – Colin Cline Dec 08 '17 at 18:00
  • agree, but will bring the requested result. Otherwise he should implement some kind of simple parser (probably even with split by the supported operators) – Nikola Yankov Dec 08 '17 at 18:02
  • I hate to see people gives down vote to an answer (that's why i like give answer a down vote just in comment and symbolic, if they have negative effect) anyways i totally agree with you but i just wanted to mentioned "IF" OP wanted to use your approach be aware and not complain later or you add some exta info to your answer about side effect. regards – Colin Cline Dec 08 '17 at 18:08