title: Javascript: can't re-assign variable within recursive function call
body: <p>In the following code I'm checking to see if a string of characters is "spellable" in a certain grid, similar to Scrabble. Letters can be used if they are directly above, below, left, or right of the current letter. For example, "bcd" should return TRUE, while "bcdn" should return FALSE. </p>
  
  <p>The functions execute exactly as I expect, <em>except</em> for on lines 32-33. From my understanding the <code>wordExists = true</code> line should override the original declaration on line 9. </p>
  
  <p>What am I missing?</p>
  
  <p>repl.it link = <a href="http://repl.it/Yhh/5" rel="nofollow">http://repl.it/Yhh/5</a></p>
  
  <pre><code>var grid = [
      ['a','b','c','d'],
      ['e','f','g','h'],
      ['i','j','k','l'],
      ['m','n','o','p']
  ];
  
  function doesWordExist(word, grid) {
      var wordExists = false;
      for (var i = 0, end = grid.length; i &lt; end; i++){
          for (var j = 0, stop = grid[0].length; j &lt; stop; j++) {
              if (word[0] === grid[i][j]) {
                  doesLetterExist(word, i, j, 1);
              }
          }
      }
      return wordExists;
  }
  
  function isMatch(letter,i,j) {
      return (
          grid[i] !== undefined &amp;&amp; 
          grid[i][j] !== undefined &amp;&amp;
          letter === grid[i][j]
      );
  }
  
  function doesLetterExist(word, i, j, depth) {
      console.log('i:' +i + " - j:" + j);
      var letter = word[depth];
      if (letter === undefined) {
          wordExists = true;
          console.log('can\'t re-define global "wordExists" variable :-(');
      }
      if (isMatch(letter,i, j + 1)) { // look right, j + 1
          doesLetterExist(word, i, j + 1, depth + 1);
      } else if (isMatch(letter, i, j -1)) { // look left, j - 1
          doesLetterExist(word, i, j - 1, depth + 1);
      } else if (isMatch(letter, i - 1, j)) { // look up, i - 1
          doesLetterExist(word, i - 1, j, depth + 1);
      } else if (isMatch(letter, i + 1, j)) { // look down, i + 1
          doesLetterExist(word, i + 1, j, depth + 1);
      }
  }
  
  doesWordExist('bcd', grid);
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Edit: I removed the word 'global' from the title. I mistakenly referred to a variable instantiated in the first function as global. It should be available and changeable from all functions within it's scope right? That is what I was referring to with 'global', it feels global to all child functions.</p>