0

As I was researching my current question this article seemed to be promising but I could not figure out for myself if it was the answer to my question. So if anyone could help my that would be terrific.

This is my Function:

function CalculateIMSUB(form) {
  var Atext = form.input_1.value;
  var Btext = form.input_2.value;
  var val = form.val.value;
  var A = eval(Atext);
  var B = eval(Btext);
  if (isNaN(A)) A = 0;
  if (isNaN(B)) B = 0;
  var answer = A - B;
  form.Answer.value = answer;
}

This is my html:

<form>
  <INPUT TYPE=TEXT name="input_1" SIZE=15>
  <INPUT TYPE=TEXT name="input_2" SIZE=10>
  <INPUT TYPE="button" VALUE="+" name="SubtractButton"
  onclick="CalculateIMSUB(this.form)">
  <INPUT TYPE=TEXT NAME="Answer" SIZE=12>
  <input type="hidden" name="val" value="" />
</form>

My question:

Can I add with "/"

For instance currently, if you were to type 10 / 5 in the input 1 text field and click calculate you would have an answer of 2 in the Answer text field. Since as we all know 10 divided by 5 equals 2. I would like to be able to type 10/5 into the input 1 text field and receive an answer of 15 which would be the equivalent to 10+5. Your help is greatly appreciated, thank you and here is my jsFiddle.

Community
  • 1
  • 1
Clint
  • 19
  • 5
  • 1
    Why would you want to represent addition with division? – Blender Feb 21 '13 at 05:09
  • and why is the plus sign there when you're subtracting the numbers?...not sure about overloading the operators, but you could do what you want with a regex – גלעד ברקן Feb 21 '13 at 05:13
  • @Blender the reason I would like to do that is this is for inventory at my work. We use the division sign to represent a different location when we are using pen and paper. – Clint Feb 21 '13 at 05:26
  • @groovy, the plus sign is there because in my function. You add everything from the input1 (5+5+5) field and subtract it by everything from the input 2 (2+2+2) field. which would give a result from the numbers I provided in the parenthesis to be 15-6 which is 9 – Clint Feb 21 '13 at 05:29

2 Answers2

1

Here's a very simple calculator implementation with prototypes and a module, no eval needed.

var calc = (function calcModule() {
  function Calculator(operation) {
    this.op = operation;
    this._init();
  }

  Calculator.prototype = {
    _init: function() {
      this.n1 = +this.op.split(/\D/)[0];
      this.n2 = +this.op.split(/\D/)[1];
      this.result = this[this.op.match(/\D/)[0]]();
    },
    '+': function() { return this.n1 + this.n2 },
    '-': function() { return this.n1 - this.n2 },
    '*': function() { return this.n1 * this.n2 },
    '/': function() { return this.n1 / this.n2 }
  };

  return function(op) {
    return new Calculator(op);
  };
}());

You can change what the symbols do if you want to. You use it like so:

console.log(calc('20-15').result); //=> 5
console.log(calc('15/5').result); //=> 3
...

Note that it's very simple, it'll only work with two numbers, but just so you get an idea...

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Thank you for the answer. But I would imagine it would be impractical for my [website](http://strubeinventory.info/home1.html). – Clint Feb 21 '13 at 06:10
  • `eval` will be even more impractical. A user could enter malicious code into any of those inputs... My example is very simple but with a bit of work you can use multiple numbers, plus this is more portable. – elclanrs Feb 21 '13 at 06:13
  • @Clint: Wanna crash your app in just a second? Enter `while(true)` into any input that it's being evaled and click "Master Calculation". BAAANG! That's why `eval` shouldn't be used for this. – elclanrs Feb 21 '13 at 06:21
  • You bring up a very good point. The website I provided is one I use with my tablet to enter inventory at my workplace. I provided the website as an example, I will shortly restrict access to the webpage so that only I can view and or submit forms. I am not a pro by any means so, I ask you if only I am using this would implementing your strategy be a sensible thing to do if I were the only one submitting data. That was what I meant when I said impractical – Clint Feb 21 '13 at 06:33
0

You could also do this:

var Atext = form.first.value.replace(/\//g,"+");
var Btext = form.second.value.replace(/\//g,"+");

Which would replace all division operators behind the scenes so that your eval will process them as addition. (So you could put 5/5/5 and get 15, or what have you.)

גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61