7 Surprising Things I Learned Writing a Fibonacci Generator in JavaScript
Eric Elliott
69516

There’s more than “just one problem” with the memoized version. If you first use it for fib(5), you get the correct sequence 0, 1, 1, 2, 3. If you then use it for fib(3) you get the sequence 1, 0, 1, 1, which impressively manages to be wrong not only in the values but in the number of values. :-)

A look at memo[] after computing fib(5) makes it clear what is wrong, if it is not apparent when you take a second look at the code: [3, 2, 1, 1, 0] rather than the [0, 1, 1, 2, 3] that you would want it to be.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.