title: JavaScript global variable becomes undefined inside function
body: <p>For some reason a JavaScript global variable becomes undefined inside function. Can't figure out why. You can copy and run the code. As you can see the global variable 'target' is defined in the first alert, but then become undefined inside the function.
  Here is the code:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Colors&lt;/title&gt;
  &lt;/head&gt;
  &lt;body onload="do_game()"&gt;
  &lt;script&gt;
  var target;            
  var guess_input;      
  var finished = false;   
  var guesses = 0;
  var colors = ['blue','brown','gold','gray','green','orange','pink','purple','red','yellow'];
  
  function do_game() {
      var random_number = Math.random() * 10;
      var index = Math.floor(random_number);
      var target = colors[index];
  alert("target = " + target);
      while (!finished) {
          guess_input = prompt("I am thinking of one those colors:\n\n"+ colors +
                                    "\n\nWhat color am I thinking of?").toLowerCase();
          guesses += 1;   
          finished = check_guess();
      }
  }
  function check_guess() {
  alert('guess_input=' + guess_input +',  target=' + target);
  return true;   
  }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>