This commit is contained in:
Ilya Kantor 2017-01-07 01:25:31 +01:00
parent ad499144a1
commit 6c9c2219ba
19 changed files with 91 additions and 54 deletions

View file

@ -0,0 +1,29 @@
function Calculator() {
var methods = {
"-": function(a, b) {
return a - b;
},
"+": function(a, b) {
return a + b;
}
};
this.calculate = function(str) {
var 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;
};
}

View file

@ -0,0 +1,26 @@
var calculator;
before(function() {
calculator = new Calculator;
});
it("calculate(12 + 34) = 46", function() {
assert.equal(calculator.calculate("12 + 34"), 46);
});
it("calculate(34 - 12) = 22", function() {
assert.equal(calculator.calculate("34 - 12"), 22);
});
it("добавили умножение: calculate(2 * 3) = 6", function() {
calculator.addMethod("*", function(a, b) {
return a * b;
});
assert.equal(calculator.calculate("2 * 3"), 6);
});
it("добавили возведение в степень: calculate(2 ** 3) = 8", function() {
calculator.addMethod("**", function(a, b) {
return Math.pow(a, b);
});
assert.equal(calculator.calculate("2 ** 3"), 8);
});