-1

I'm new in JavaScript and I'm trying to create a simple calculator with a separated function of operation example for sum and subtraction function

fonction calculator() {
  this.addition = function(param1, param2) {
    .........
  }
  this.subtraction = function(param1, param2) {
    .........
  }
}

and like this for division without using an eval function .

i have made a lot of try without any result my code : https://codepen.io/anon/pen/oRderx

zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • Please note that Java and JavaScript are two completely different languages. This has nothing to do with Java. – Ivar May 25 '19 at 21:08
  • `return param1 + param2` in the `addition` function and `return param1 - param2` in the `subtraction` function? – Bergi May 25 '19 at 21:11
  • Your code does not appear to include the addition or subtraction methods. *What have you tried?* – GalacticCowboy May 25 '19 at 21:13

1 Answers1

1

Separate the display logic from the operational logic.

Extremely simple example:

const input = '22*7';
const operands = input.split('*');
const result = operands[0] * operands[1];
console.log(result);

Because you have an interface for this to be defined, I would suggest not splitting the string, but having your input create an array of 'operands' and 'operators' that can then build a string from, with the result being easily consumable for calculating the result, for instance

Simple example: `[22, 'x', 15, '*', 2];

The output string could then easily be shown using .join('')

const view = [55, '+', 22, '*', 2].join('');
console.log(view);

The challenge here will likely be determining order of operation. A recursive solution might prove useful for this.

msg45f
  • 695
  • 11
  • 21