我搜索数组中的任何单词。如果这个词存在于数组中,我想返回true,否则我想返回false。我已经尝试了在if条件下返回,但它不起作用。
如果条件是我想将found返回给wordCheck函数。
if (str == searching) {
found = true;
return found;
}它是我嵌套的整个函数。
let boggleboard = [
["G", "I", "Z"],
["U", "E", "K"],
["Q", "S", "Y"],
];
var word = "GI";
// console.log(boggleboard);
var x = checkWord(boggleboard, word);
console.log("x", x);
console.time("checkWord");
function checkWord(boggle, searching) {
var found = false;
var visited = new Array(boggle.length)
.fill(0)
.map(() => new Array(boggle.length).fill(0));
var str = "";
boggle.map(function (strchk1, index) {
boggle[index].map(function (strchk2, index2) {
findWordsUtil(boggle, visited, index, index2, str, searching);
});
});
return found;
function findWordsUtil(boggle, visited, i, j, str, searching) {
visited[i][j] = 1;
str = str + boggle[i][j];
if (str == searching) {
found = true;
return found;
}
boggle.map(function (strchk1, row) {
boggle[row].map(function (strchk2, col) {
if (row >= 0 && col >= 0 && !visited[row][col]) {
findWordsUtil(boggle, visited, row, col, str, searching);
}
});
});
// if (found) return true;
// else {
str = "" + str[str.length - 1];
visited[i][j] = 0;
// }
}
}```发布于 2021-05-06 03:27:58
你的问题是关于返回一个嵌套的函数,所以下面我给出了一些简单的例子,其中包括注释。简单地说,在此过程中的每一步,都必须将值从内部函数返回到下一层,直到返回到原始请求为止。你必须设置原始的请求,这样它才能“捕捉”结果。例如
simpleCalculator(value, a, b);将执行函数,但不会捕获返回值。在这种情况下,假设您需要做的所有工作都在函数本身内完成。如果您希望结果在全局可见,则需要如下内容:
const result = simpleCalculator(value, a, b); 一个常见的错误是将返回语句放在错误的位置。
下面是一些包含注释的示例。
function simpleCalculator(value,a,b) {
switch(value) {
case 'add' : return add(a,b);
case 'subtract': return subtract(a,b);
case 'multiply': return multiply(a,b);
case 'divide' : divide(a,b); break; // There's no return here, so the result isn't visible globally.
default : return 'Invalid input';
}
function add(a,b) {
let c = a + b; // Here we do the math, store it in 'c', then return 'c'.
return c;
}
function subtract(a,b) {
return a - b; // Here we do the math and return it directly. There's no need to store it in a variable if we're only going to return the results.
}
function multiply(a,b) {
a * b; // Here we do the math but don't return it, so the value of 4 * 5 isn't visible outside this inner scope.
}
function divide(a,b) {
return a / b; // Here we return the result to simpleCalculator(), but simpleCalculator() didn't return it to variable division, so division is undefined.
}
}
simpleCalculator('add', 4, 5); // The result is returned, but we haven't set up a variable to capture it nor a function to display it.
console.log(simpleCalculator('add', 4, 5)); // Here we display the result to the console, but don't capture it.
let addition = simpleCalculator('add', 4, 5); // Note that we have returns in both simpleCalculator() and add(), so the value is visible in 'addition'
console.log('Addition:', addition);
let subtraction = simpleCalculator('subtract', 4, 5); // Note that we returned the result in both simpleCalculator() and subtract() without assigning the value to an inner variable.
console.log('Subtraction', subtraction);
let multiplication = simpleCalculator('multiply', 4, 5); // Defaults to undefined because of a missing return statement in multiply().
console.log('Multiplication:', multiplication);
let division = simpleCalculator('divide', 4, 5); // Defaults to undefined because of a missing return statement in simpleCalculator().
console.log('Division:', division);
https://stackoverflow.com/questions/67395401
复制相似问题