Функция ниже создает калькулятор, который сначала поддерживает только сложение и вычитание двух чисел, но может быть расширен с помощью this.addMethod для размещения других операторов, таких как «/», «*» и «** ".
Однако, когда я добавляю новый калькулятор через конструктор (" new ") и добавляю в него новые операции, операция power (" ** ") работает правильно, только если она вызывается первой.
Полный код ниже:
function Calculator() {
this.supportedOperators = ['+', '-'];
this.supportedMethods = [
(a, b) => a + b,
(a, b) => a - b
];
this.calculate = (str) => {
for (operator of this.supportedOperators) {
if (str.includes(operator)) {
let delimiter = operator.length; // for '**' operator
let firstOperand = str.slice(0, str.indexOf(operator));
let secondOperand = str.slice(str.indexOf(operator) + delimiter);
return this.supportedMethods[this.supportedOperators.findIndex(item => item === operator)]
(+firstOperand, +secondOperand);
/* check the supported operators, then use it on operands
A mess here, but i tried my best to make it more readable and understandable */
} else console.log('Unsupported operation');
}
};
this.addMethod = (operator, method) => {
this.supportedOperators.push(operator);
this.supportedMethods.push(method);
}
}
let powerCalc = new Calculator;
powerCalc.addMethod("**", (a, b) => a ** b); // works fine
powerCalc.addMethod("*", (a, b) => a * b);
powerCalc.addMethod("/", (a, b) => a / b);
let result = powerCalc.calculate("4 ** 3"); // 64, as should be(other values also supported)
console.log(result);
Однако, если я изменю порядок в последней последовательности кода, так что добавление операции "**" больше не будет первым, как это:
function Calculator() {
this.supportedOperators = ['+', '-'];
this.supportedMethods = [
(a, b) => a + b,
(a, b) => a - b
];
this.calculate = (str) => {
for (operator of this.supportedOperators) {
if (str.includes(operator)) {
let delimiter = operator.length; // for '**' operator
let firstOperand = str.slice(0, str.indexOf(operator));
let secondOperand = str.slice(str.indexOf(operator) + delimiter);
return this.supportedMethods[this.supportedOperators.findIndex(item => item === operator)]
(+firstOperand, +secondOperand);
/* check the supported operators, then use it on operands
A mess here, but i tried my best to make it more readable and understandable */
} else console.log('Unsupported operation');
}
};
this.addMethod = (operator, method) => {
this.supportedOperators.push(operator);
this.supportedMethods.push(method);
}
}
let powerCalc = new Calculator;
powerCalc.addMethod("*", (a, b) => a * b); //
powerCalc.addMethod("/", (a, b) => a / b); // those two work fine regardless of order
powerCalc.addMethod("**", (a, b) => a ** b); // changed the order, no works fine
let result = powerCalc.calculate("4 ** 3"); // throws NaN with any value
console.log(result);
Операция питания теперь возвращает NaN.
Я затрудняюсь понять это. Пожалуйста, помогите.