How to return nested function in javascript?

Viewed 111

I search for any words in an array. If the word exists in the array I wanna return true, otherwise, I wanna return false. I already tried return in if conditions but it did not work.

If condition I want to return found to wordCheck function.

if (str == searching) {
      found = true;
        return found;
    }

It is my nested entire function.

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;
    // }
  }
}```
1 Answers

Your question is on returning a nested function, so below I've given some simple examples of ones that work and ones that don't with comments included. The short answer is that for every step along the way, you have to return the value from the inner function to the next level up until you've returned all the way to the original request. And you have to set up the original request so it 'catches' the result. For example

 simpleCalculator(value, a, b);

will execute the function, but won't capture the returned value. In this situation, it's assumed that all the work you need to do is completed within the functions, themselves. If you wish the result to be visible globally, you need something like:

const result = simpleCalculator(value, a, b); 

One common mistake is to put the return statement in the wrong place.

Following are some examples with comments included.

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);

Related