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

The Archive Base Latest Questions

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

Here is my dilemma. I’ve got this section of code: var list_of_numbers = new

  • 0

Here is my dilemma.

I’ve got this section of code:

var list_of_numbers = new Array();

function AddToArray(func)
{
    // Add to the *beginning* of the array
    // essentially reversing the order

    list_of_numbers.unshift(func);
}

function DisplayNumber(num)
{
    document.write(num);
}

for(var i=0;i<5;++i)
{
   AddToArray(function() { DisplayNumber(i); });
}

for(var i=0;i<5;++i)
{
   list_of_numbers[i]();
}​

What is supposed to happen is that 5 inline functions will be added to the array – each taking a copy of i. However this does not happen.

Expected output:

43210

Actual output:

01234

  • 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-16T17:56:17+00:00Added an answer on May 16, 2026 at 5:56 pm

    You have two separate issues, both related to scope.

    var list_of_numbers = new Array(); 
    function AddToArray(func) 
    { 
        // Add to the *beginning* of the array
        // essentially reversing the order 
        list_of_numbers.unshift(func); 
    } 
    
    function DisplayNumber(num) 
    { 
        document.write(num); 
    } 
    for(var i=0;i<5;++i) 
    { 
        (function(i) 
         { 
             AddToArray(function(){ DisplayNumber(i); });
         })(i); 
    } 
    
    for(var j=0;j<5;++j) 
    { 
        list_of_numbers[j](); 
    }​
    
    1. The anonymous function you’re passing to AddToArray is bound to the variable i, not the current value. To address this, we create a new function, and pass in the current i.

    2. JavaScript has function scope, so when you re-declare i in the second loop, you’re still modifying the same variable. Thus, we rename it to j.

    If only the first were an issue, you would get 55555, since all functions would use the same i, at that point 5. However, since you reuse i for the second index, i is set to the current loop index.

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

Sidebar

Related Questions

Ok here's my dilemma. here's this code I have: If e.KeyCode = Keys.A Then
Interesting dilemma here. I need the ability for users to add links from their
I've got an odd little dilemma in this jQuery slideshow plugin that I am
HI, Here's my dilemma. The below code is obviously wrong but it give you
I'm running into a dilemma here. The Firefox Add-Ons SDK only allows PUT and
Here's my dilemma. I'm using a RESTful ASP.NET service, trying to get a function
Ok, here is my dilemma I have a database set up with about 5
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here's my dilemma - I have a two-column list of elements (styled <div> s)

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.