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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:29:39+00:00 2026-05-25T09:29:39+00:00

I guess its to stop browsers getting nailed all the time by duff code

  • 0

I guess its to stop browsers getting nailed all the time by duff code but this:

        function print(item) {
            document.getElementById('output').innerHTML = 
               document.getElementById('output').innerHTML
               + item + '<br />';
        }

        function recur(myInt) {
            print(myInt);
            if (int < 10) {
                for (i = 0; i <= 1; i++) {
                    recur(myInt+1);
                }
            }
        }

produces:

0
1
2
3
4
5
6
7
8
9
10
10

and not the big old mess I get when I do:

        function recur(myInt) {
            print(myInt);
            if (int < 10) {
                for (i = 0; i <= 1; i++) {
                    var x = myInt + 1;
                    setTimeout("recur("+x+")");
                }
            }
        }

Am I missing something or is this how you do recursion in JS? I am interested in navigating trees using recursion where you need to call the method for each of the children.

  • 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-25T09:29:39+00:00Added an answer on May 25, 2026 at 9:29 am

    You are using a global variable as loop counter, that’s why it only loops completely for the innermost call. When you return from that call, the counter is already beyond the loop end for all the other loops.

    If you make a local variable:

    function recur(int) {
        print(int);
        if (int < 10) {
            for (var i = 0; i <= 1; i++) {
                recur(int + 1);
            }
        }
    }
    

    The output is the same number of items as when using a timeout. When you use the timeout, the global variable doesn’t cause the same problem, because the recursive calls are queued up and executed later, when you have exited out of the loop.

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

Sidebar

Related Questions

This is may be not program question, but i guess its program logic question.
I thought my code would work, but I guess not. I'm just trying to
I keep getting this weird error message every time I'm testing my app on
I have this line of code: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil]; Works fine
I've searched far and wide for its meaning. My guess is that I somehow
On Windows Mobile (but I guess it's the same on Windows) in a native
I guess this is an odd one, and the answer is most likely it
I guess I'll illustrate with an example: In this game you are able to
I'm new at C#, but I guess I'm using a simple command in Directory.CreateDirectory,
I am getting this error, randomly when I start my application (and it's sub

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.