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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:46:02+00:00 2026-05-15T22:46:02+00:00

I have a function, in which there is a loop which calls up the

  • 0

I have a function, in which there is a loop which calls up the function.

function displayItem(item, isChild)
{
    if (isChild)
    {
        writeOutput('<li>' & item.name & '</li>');
    }
    else
    {
        writeOutput('<li>' & item.name);
    }
    try
    {
        if (item.hasChild)
        {
            writeOutput('<ul>');
            numberOfItems = item.numChildren;
            for (x=1;x LT numberOfItems;x++)
            {
                displayItem(item.child[x], true);
            }
            writeOutput('</ul>');
        }
    }
    catch(Exception e){}
    writeOutput('</li>');
} 

After the function is executed, the loop continues off of the value of x from that execution, rather than picking up where it left off before.

For instance: x is 3. displayItem is passed “item.child[3]” and true. It works through the function, enters the loop, performs the loop up to x = 4, then falls out of the loop. The function ends and (from what I understand) returns back to the point where x should be 3. Instead of picking up from 3, adding one (making it 4) and then performing the loop again, it picks up from 4 (the value from the “internal” recursively called loop).

I know that sounds incoherent, but I can’t think of any other way to explain it.

Is there something I’m doing wrong, or is this just a fact of life and something I have to work around?

UPDATE:
After looking at it more, it appears as though the earliest loop is exiting early. Using a local variable for ‘x’ fixed the counting issue, but the loop just exits at 4 if looking at the previous example. It leaves before the condition is met. Any 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-15T22:46:02+00:00Added an answer on May 15, 2026 at 10:46 pm

    The problem is this line:

    numberOfItems = item.numChildren;
    

    When returning from the second call, this value is not changed back to the proper value. So if numberOfItems is set to 4 when the function is called up by itself, after it has completed and returned to the original instance of the function, numberOfItems is still 4.

    this code works:

    function displayItem(item, isChild)
    {
        var x = 1;
        if (isChild)
        {
            writeOutput('<li>' & item.name & '</li>');
        }
        else
        {
            writeOutput('<li>' & item.name);
        }
        try
        {
            if (item.hasChild)
            {
                writeOutput('<ul>');
                for (x=1;x LT item.numChildren;x++)
                {
                    displayItem(item.child[x], true);
                }
                writeOutput('</ul>');
            }
        }
        catch(Exception e){}
        writeOutput('</li>');
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been proposals for C++ delegates which have lower overhead than boost::function :
I have a function which I have to loop it a few times but
I have a function which takes two arguments, the ID of an item (wine)
I have an R function which calls a webserver database through it's API using
I'm using VB 2010 Express. I have a loop that calls a function named
There is a JavaScript function, of which I have zero control of the code,
I have a ColdFusion function foo which takes three args, and the second two
Is there a firefox plugin which functions as an SVN client? I have checked
hi i have function which is called by tinker listbox so i cannot return
Can I´m asking for advice. I have function which should return javascript object function

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.