0

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.

7
  • 2
    Is this function being called while the page is loading, or after? Commented Feb 19, 2014 at 0:30
  • Well, what is isWord? Is there an error in the JavaScript console? Why are you writing ==true? Commented Feb 19, 2014 at 0:31
  • it is being called as the page is loading Commented Feb 19, 2014 at 0:31
  • isWord works in other parts of the program, it just determines if the word is a valid 3 letter word by looking at a list i put into the code. Commented Feb 19, 2014 at 0:31
  • 1
    mm to check the page loading issue change the document.write to console.log.. im curious if this is the issue Commented Feb 19, 2014 at 0:33

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.