taken from this link that I came across trying to figure it out.
Here's the function (modified bit to try to help yourself understand):
(function(){ fibonacci = (function () { var cache = {}; return function (n) { var cached = cache[n]; if (cached) { console.log('already in the ', cache); return cached; } if (n <= 1) { console.log('no 0s or 1s, ', n); return n; } console.log('a brand new ', n, 'consider yourself cached'); cache[n] = fibonacci(n - 2) + fibonacci(n - 1); console.log('current cache: ', cache); return cache[n]; }; }()); fibonacci(20); })();
I changed it a bit to try to help myself understand, but I am lost because the output goes to 0, then it increases from 0. I would think that in this statement:
cache[n] = fibonacci(n - 2) + fibonacci(n - 1);
fibonacci(n - 2) will be evaluated, then fibonacci(n - 1) right after that.
But even if that were the case, I don’t understand how JavaScript will add these two functions together.
Can someone help me understand how this works, or at least you can help me restructure it in a way that might be a little easier to understand?
Here's the conclusion:
a brand new 20 consider yourself cached a brand new 18 consider yourself cached a brand new 16 consider yourself cached a brand new 14 consider yourself cached a brand new 12 consider yourself cached a brand new 10 consider yourself cached a brand new 8 consider yourself cached a brand new 6 consider yourself cached a brand new 4 consider yourself cached a brand new 2 consider yourself cached no 0s or 1s, 0 no 0s or 1s, 1 current cache: Object {2: 1} a brand new 3 consider yourself cached no 0s or 1s, 1 already in the Object {2: 1} current cache: Object {2: 1, 3: 2} current cache: Object {2: 1, 3: 2, 4: 3} a brand new 5 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3} already in the Object {2: 1, 3: 2, 4: 3} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} a brand new 7 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} a brand new 9 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} a brand new 11 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} a brand new 13 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} a brand new 15 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} a brand new 17 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} a brand new 19 consider yourself cached already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181} current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181, 20: 6765}
Thank you, I know that recursion is probably the big question about the Nob, and I used it several times, but understanding how it works makes my head spin.