The following function proposed by “Doug Crockford: JavaScript: The Good Parts.” works great.
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));
But let’s see in detail what happens to the line 4
console.debug(memo, c, memo[c]);
It shows the following result which is opposite to what would be expected.
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
Some ideas?
It’s a problem with Chrome’s console output. Chrome seems to dynamically update the output to reflect the content of the array somehow. If you run it in Firebug, the output is:
Also it makes sense when you try to go through the code in your head:
console.debugis the first statement infib. The first timefibis called,memois[0, 1]as there haven’t been made any changes to the array.cis3, so you getundefined. So in the first invocation,memocannot be[0, 1, 1, 2]no matter what the console shows.Some JavaScript consoles seem to show such a behaviour (in one way or an other), when you log references to arrays or objects. In these cases it is often better to set breakpoints and go through the code step by step.
Update: It seems Firebug fixed this issue (if it ever existed), but it still seems to exist in Firebug light.