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