首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在javascript中返回嵌套函数?

如何在javascript中返回嵌套函数?
EN

Stack Overflow用户
提问于 2021-05-05 12:58:25
回答 1查看 74关注 0票数 2

我搜索数组中的任何单词。如果这个词存在于数组中,我想返回true,否则我想返回false。我已经尝试了在if条件下返回,但它不起作用。

如果条件是我想将found返回给wordCheck函数。

代码语言:javascript
运行
复制
if (str == searching) {
      found = true;
        return found;
    }

它是我嵌套的整个函数。

代码语言:javascript
运行
复制
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;
    // }
  }
}```
EN

回答 1

Stack Overflow用户

发布于 2021-05-06 03:27:58

你的问题是关于返回一个嵌套的函数,所以下面我给出了一些简单的例子,其中包括注释。简单地说,在此过程中的每一步,都必须将值从内部函数返回到下一层,直到返回到原始请求为止。你必须设置原始的请求,这样它才能“捕捉”结果。例如

代码语言:javascript
运行
复制
 simpleCalculator(value, a, b);

将执行函数,但不会捕获返回值。在这种情况下,假设您需要做的所有工作都在函数本身内完成。如果您希望结果在全局可见,则需要如下内容:

代码语言:javascript
运行
复制
const result = simpleCalculator(value, a, b); 

一个常见的错误是将返回语句放在错误的位置。

下面是一些包含注释的示例。

代码语言:javascript
运行
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67395401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档