<!DOCTYPE HTML>
<html>
<head>
<!-- Asya Bakhnar -->
<!-- http://learn.javascript.ru/task/calculator-extendable -->
<meta charset="utf-8">
<script src="https://js.cx/test/libs.js"></script>
<script src="test.js"></script>
</head>
<body>
<script>
'use strict';
class Calculator {
constructor() {
this.operation = {
// Честно говоря, не видел, чтобы так методы объявляли, но прикольно!
// Я имею в виду, что обычно все-так литерал строки в качестве ключа
// не смешивают с новым синтаксисом объявления методо. Но это ок!
'-'(a, b) {
return a - b;
},
'+'(a, b) {
return a + b;
}
};
}
calculate(str) {
let separateSymbols = str.split(' '),
sum = 0;
return sum = this.operation[separateSymbols[1]](+separateSymbols[0], +separateSymbols[2]);
}
addMethod(name, func) {
this.operation[name] = func;
}
}
</script>
</body>
</html>
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);
});