Apparently contradictory behavior of Javascript function
<p>The following function proposed by "Doug Crockford: JavaScript: The Good Parts." works great.</p>

<pre><code>var fibonacci = function () {
  var memo = [0, 1];
  var fib = function (c) {
    console.debug(memo, c, memo[c]);
    result = memo[c];
    if (typeof result !== 'number'){
      result = fib(c - 1) + fib(c - 2);
      memo[c] = result;
    }
    return result;
  };
  return fib;
}();

console.log(fibonacci(3));
</code></pre>

<p>But let's see in detail what happens to the line 4</p>

<pre><code>console.debug(memo, c, memo[c]);
</code></pre>

<p>It shows the following result which is opposite to what would be expected.</p>

<pre><code>memo,        c, memo[c]
[0, 1, 1, 2] 3  undefined  //contradictory behavior because I expect to have memo = [0, 1]
[0, 1, 1, 2] 2  undefined  //contradictory behavior
[0, 1, 1, 2] 1  1
[0, 1, 1, 2] 0  0
[0, 1, 1, 2] 1  1
2
</code></pre>

<p>Some ideas?</p>
