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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:32:52+00:00 2026-05-25T01:32:52+00:00

I seem to somehow be losing the value of a variable im setting… What

  • 0

I seem to somehow be losing the value of a variable im setting…

What im trying to do is not so important, so I’ve set up a (Well commented) jsFiddle to show you what im getting. Also the code is below.

If anyone can see whats going on any help is appreciated 🙂

See jsFiddle > http://jsfiddle.net/qNWuV/4/ < Recommend you take a look here

var habs = ["417,77", "410,363", "388,433", "262,435", "262,210", "391,101", "384,183", "61,114", "331,171", "164,433", "361,248", "302,329", "154,307", "410,350", "173,298", "308,429"]; //just an array of co-ords for another part of my app. Only the .length is used below.

//############################
// NOTE: as this problem depends on random numbers you MAY not see it. If "undefined" is ANYWHERE in the Result, the problem is occurring, otherwise re-run the code.
//############################


function link_habs(habs) {
    var test2 = '';
    var hab_length = habs.length;
    for (var e in habs) {
        var hab_link_1 = get_link(hab_length, e + ',');
        var hab_link_2 = get_link(hab_length, e + ',' + hab_link_1);
        document.write('<br /><br />each1: ' + hab_link_1); //Variable lost?
        document.write('<br />each2: ' + hab_link_2 + '<br />'); //Variable lost?
        test2 += e + ':' + hab_link_1 + ',' + hab_link_2 + '<br />';
    }
    document.write('<br /><br /><br />' + test2);
}

function get_link(count, not) {
    var nots = not.split(',');
    for (var i in nots) { nots[i] = parseInt(nots[i], 10); }
    var hab_link = Math.floor(Math.random() * count);
    if (nots.indexOf(hab_link) === -1) {
        document.write('<br />returned: ' + hab_link); //Variable is intact HERE
        return hab_link;
    } else {
        get_link(count, not);
    }
}

link_habs(habs);

Cheers
Charlie

  • 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-25T01:32:53+00:00Added an answer on May 25, 2026 at 1:32 am

    You are not returning the value from the recursive call.

    Change:

    get_link(count, not);
    

    into:

    return get_link(count, not);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow, Tortoise against Subversion can't seem to figure out that the source is not
I'm trying to read values from the registry but somehow I seem to be
I somehow seem to have jacked up a setting in my QtCreator project. I
I'm trying to change a variable depending on what it's current value is: window.addEventListener(popstate,
so I'm not well versed in overriding hashCode and I seem to have some
I'm busy implementing the wurfl api using PHP, but somehow just can't seem to
I seem to make this mistake every time I set up a new development
Somehow I can't seem to get string replacement within a foreach loop in C#
How to do this in DirectX? glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); Somehow i just cant seem
I am somehow unable to determine whether a string is newline or not. The

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.