我有一个小计算器应用程序。
return 语句位于 if 语句内部,用于检查变量是否等于“null”。如果变量等于“null”,它应该返回调用函数的输入。然而,无论输入是什么,它总是返回未定义。
这是代码:
const input = ['1', 'substract', '3'];
const findNextOperation = (input) => {
let operation;
let ind;
//First * or /
ind = input.findIndex(el => /multiply|divide/.test(el));
//Then + or -
if (ind === -1) ind = input.findIndex(el => /add|substract/.test(el));
//No operations left
if (ind === -1) return null;
operation = input[ind];
return { operation, ind };
}
const performOperation = (a, b, operation) => {
a = Number(a);
b = Number(b);
switch (operation) {
case 'add':
return a + b;
case 'substract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return;
}
}
const calculate = (input) => {
const operation = findNextOperation(input);
if (operation === null) {
console.log('here', input[0]) // Logs "here -2" to the console
return input[0]; // Returns "undefined"
}
const operationOutcome = performOperation(input[operation.ind - 1], input[operation.ind + 1], operation.operation);
calculate(input.toSpliced(operation.ind - 1, 3, operationOutcome));
}
console.log(calculate(input)); // Logs "undefined" to the console
我不知道哪里出了问题。
您已经通过在
calculate
函数中调用 calculate
实现了递归方法。
但是,第二次调用的返回值丢失了。
您应该更改以下内容
calculate(input.toSpliced(...
// to
return calculate(input.toSpliced(...
const findNextOperation = (input) => {
let operation;
let ind;
//First * or /
ind = input.findIndex(el => /multiply|divide/.test(el));
//Then + or -
if (ind === -1) ind = input.findIndex(el => /add|substract/.test(el));
//No operations left
if (ind === -1) return null;
operation = input[ind];
return { operation, ind };
}
const performOperation = (a, b, operation) => {
a = Number(a);
b = Number(b);
switch (operation) {
case 'add':
return a + b;
case 'substract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return;
}
}
const calculate = (input) => {
const operation = findNextOperation(input);
if (operation === null) {
return input[0]; // Returns "undefined"
}
const operationOutcome = performOperation(input[operation.ind - 1], input[operation.ind + 1], operation.operation);
return calculate(input.toSpliced(operation.ind - 1, 3, operationOutcome));
}
console.log(calculate(['1', 'substract', '3']));
console.log(calculate(['10', 'divide', '3']));
您从计算函数中获得“未定义”结果的主要原因是您没有从函数中所有可能的代码路径返回任何值。具体来说,在该行之后:
calculate(input.toSpliced(operation.ind - 1, 3, operationOutcome));
您没有返回递归调用计算的结果。
但是,还有一些问题:
您的代码中有拼写错误。 JavaScript 数组有一个名为 splice 的方法,而不是 toSpliced。但 splice 直接修改数组并返回删除的元素,这不是你想要的。相反,您可以创建一个自定义方法来实现您想要的,或者以不同的方式处理拼接。
在数组上调用 splice 时,您需要确保删除正确的元素,然后将结果插入到它们的位置。
如果您希望计算函数继续递归,然后最终在达到基本情况后返回一个值,则需要确保返回递归调用的结果。
考虑到这些事情,这是代码的修订版本:
const input = ['1', 'substract', '3'];
const findNextOperation = (input) => {
let operation;
let ind;
//First * or /
ind = input.findIndex(el => /multiply|divide/.test(el));
//Then + or -
if (ind === -1) ind = input.findIndex(el => /add|substract/.test(el));
//No operations left
if (ind === -1) return null;
operation = input[ind];
return { operation, ind };
}
const performOperation = (a, b, operation) => {
a = Number(a);
b = Number(b);
switch (operation) {
case 'add':
return a + b;
case 'substract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return;
}
}
const calculate = (input) => {
const operation = findNextOperation(input);
if (operation === null) {
return input[0];
}
const operationOutcome = performOperation(input[operation.ind - 1], input[operation.ind + 1], operation.operation);
const newInput = [...input];
newInput.splice(operation.ind - 1, 3, operationOutcome);
return calculate(newInput);
}
console.log(calculate(input));
此代码现在将为所提供的输入正确返回“-2”。