-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

3
  • Please note that Java and JavaScript are two completely different languages. This has nothing to do with Java. Commented May 25, 2019 at 21:08
  • return param1 + param2 in the addition function and return param1 - param2 in the subtraction function? Commented May 25, 2019 at 21:11
  • Your code does not appear to include the addition or subtraction methods. What have you tried? Commented May 25, 2019 at 21:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can u give please a simple exemple in my code here :codepen.io/anon/pen/oRderx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.