Reputation: 145
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.
Upvotes: 0
Views: 50
Reputation: 4161
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;
}
Upvotes: 1