Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:16:19+00:00 2026-05-17T00:16:19+00:00

Beginner in JS :) needs an explanation of code piece from Crockford’s book ,

  • 0

Beginner in JS 🙂 needs an explanation of code piece from Crockford’s book, section 4.15:

var memoizer = function (memo, fundamental) {
    var shell = function (n) {
        var result = memo[n];
        if (typeof result !== 'number') {
            result = fundamental(shell, n);
            memo[n] = result;
        }
        return result;
    };
    return shell;
};

var fibonacci = memoizer([0, 1], function (shell, n) {
    return shell(n - 1) + shell(n - 2);
});

Question: How do we calculate fibonacci(15), and if it is simple fibonacci(15) call, then how it works in detail?

Thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:16:20+00:00Added an answer on May 17, 2026 at 12:16 am

    As the comments to your question suggest, you should walk through the code in a debugger to get a good understanding of what’s going if you can’t follow the explanation in the book. But I’ll give you a brief overview of what’s happening:

    What’s being demonstrated is ‘memoisation’ which is a common optimisation technique used in functional programming. A function is said to be pure if the result depends only on the arguments passed into it. So, if a function is pure you can cache the result based on the arguments – this technique is called memoisation. You would do this if a function is expensive to calculate and is called multiple times.

    The classical example used to demonstrate this (as here) is generating Fibonacci numbers. I’m not going to go through how those are worked out, but basically as you go to higher and higher numbers you repeat yourself more and more as each number is calculated from the preceeding two numbers. By memoising each intermediate result you only have to calculate them once hence making the algorithm much faster (much, much faster as you go higher up the sequence).

    As far as this code is concerned the memoizer takes two parameters – ‘memo’ which is the cache. In this case it’s going in with the first two values already filled in the ‘[0,1]’ – these are the first two Fibonacci numbers.

    The second parameter is the function to which the memoisation will be applied. In this case a recursive Fibonacci function:

    function (shell, n) {
    return shell(n – 1) + shell(n – 2);
    }

    i.e. the result is the sum of the previous two numbers in the sequence.

    The memoizer first of checks to see if it already has a cached result. If it does it returns that immediately. If not it calculates the result and stores it in the cache. Without doing this it’d be repeating itself again and again and rapidly gets impossibly slow once to get to the higher numbers in the sequence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a total beginner in JS/XML. I have this simple code that needs
I have a question regarding this code. I'm a beginner and enjoy learning C#.
I am writing a test code (I'm just a beginner) where I need to
I'm not a beginner at C# but I really need to increase my understanding,
Ok I am a total beginner with the Solaris Operating system and I need
I am a beginner SQL user (not formally trained; OJT only) and need some
A beginner question, bear with me: I'm just wondering under what circumstances one should
A beginner's question. I'm building a .swf with Flex Ant. To my .swf I
What database should a beginner use in the C language? Can I use MySQL?
I am a complete beginner trying to develop for FCKeditor so please bear with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.