title: I'm unsure of how to solve this anonymous function closure question from my textbook
body: <p>The question is this:</p>
  
  <p><em>"One of your colleagues has written the following code routine, but it isn’t working the way she expected. Can you explain why? How would you fix the code so that it gives the output your colleague was expecting?"</em></p>
  
  <pre><code>var output = [];
  
  for (var i = 0; i &lt; 5; i++) {
    output[i] = function () {
      console.log(i);
    }
  }
  
  output[0](); // logs 5, not the expected 0
  output[1](); // logs 5, not the expected 1
  output[2](); // logs 5, not the expected 2
  output[3](); // logs 5, not the expected 3
  output[4](); // logs 5, not the expected 4
  </code></pre>
  
  <p>Going through this chapter I had discovered a mistake in a previous example that was easy enough to pick up on. So analyzing the exercise worries me.</p>
  
  <p>I understand closures are functions that can access parent variables. Rather than solving this for me, I wanted to know if the community point me in the right direction to solving this.</p>
  
  <p>I have done research on this topic and I've completed 2 other tutorials on closures, I just don't understand this question. 
  Thanks</p>