I am writing a basic boggle program. My recursive method is as follows:
function findWords(str, i, j) {
if (j<0 || i<0 || i>=4 || j>=4)
return;
if (marked[i][j])
return;
marked[i][j] = true;
str = str+Board[i][j];
document.write(str + " ");
if(str.length>4)
return;
if(isWord(str)==true)
document.write(str);
for (var ii = -1; ii<=1; ii++)
for (var jj = -1; jj<=1; jj++)
findWords(str, i+ii, j+jj);
marked[i][j] = false;
}
However it only goes through one time. It stops at "if(isWord(str)==true) document.write(str);". When I comment out this portion of the code, the method works almost as expected. Is there a reason that the program would just stop at this point? Any advice is appreciated guys.
Asken
8,15711 gold badges50 silver badges85 bronze badges
1 Answer 1
Try placing some brackets in there as follows:
function findWords(str, i, j){
if(j<0 || i<0 || i>=4 || j>=4) return;
if (marked[i][j]) return;
marked[i][j] = true;
str = str+Board[i][j];
document.write(str + " ");
if(str.length>4) return;
if(isWord(str)==true)
{
document.write(str);
}
for(var ii = -1; ii<=1; ii++)
{
for (var jj = -1; jj<=1; jj++)
{
findWords(str, i+ii, j+jj);
}
}
marked[i][j] = false;
}
answered Feb 19, 2014 at 0:34
JosephGarrone
4,1613 gold badges42 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
isWord? Is there an error in the JavaScript console? Why are you writing==true?