en.javascript.info/1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/solution.js
Ilya Kantor 20784e7f26 up
2017-02-19 01:41:36 +03:00

25 lines
414 B
JavaScript

function Calculator() {
let methods = {
"-": (a, b) => a - b,
"+": (a, b) => a + b
};
this.calculate = function(str) {
let split = str.split(' '),
a = +split[0],
op = split[1],
b = +split[2]
if (!methods[op] || isNaN(a) || isNaN(b)) {
return NaN;
}
return methods[op](a, b);
}
this.addMethod = function(name, func) {
methods[name] = func;
};
}