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 6572359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:59:42+00:00 2026-05-25T14:59:42+00:00

The following function proposed by Doug Crockford: JavaScript: The Good Parts. works great. var

  • 0

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?

  • 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-25T14:59:43+00:00Added an answer on May 25, 2026 at 2:59 pm

    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:

    [0, 1]    3 undefined
    [0, 1]    2 undefined
    [0, 1]    1 1
    [0, 1]    0 0
    [0, 1, 1] 1 1
    2
    

    Also it makes sense when you try to go through the code in your head: console.debug is the first statement in fib. The first time fib is called, memo is [0, 1] as there haven’t been made any changes to the array. c is 3, so you get undefined. So in the first invocation, memo cannot 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.

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

Sidebar

Related Questions

The following function works in PHP > 5.3 but errors out in older versions.
In his Good Parts, Crockford suggests that 'new' should never be used. To follow
The following function works perfectly: protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo, string
The following function gets the target element in a dropdown menu: function getTarget(evt){ var
The following function to change image source on mouse hover/mouseout works in Chrome: $(document).ready(function()
The following function rewrites urls from news and product titles that contain all sorts
The following function is designed to check whether this row in this tables exists.
The following function that reports the current line-number (and time) which I would like
The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value
/** The following function checks the red black tree black height * @param n

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.