Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Variable losing its value and "becoming" undefined

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

like image 309
Charlie Sheather Avatar asked Aug 28 '11 05:08

Charlie Sheather


People also ask

Why is my variable returning undefined JavaScript?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

Is variable undefined JavaScript?

A variable is said to be 'undefined' if it has been declared, but no value has been given to it. Contrary to this, 'null' is a value that can be assigned to a variable and represents 'no value'. Therefore, 'undefined' is a variable type whereas 'null' is an object value.

What is the problem with JavaScript?

These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)

Can we return undefined in JavaScript?

An undefined variable or anything without a value will always return "undefined" in JavaScript. This is not the same as null, despite the fact that both imply an empty state. You'll typically assign a value to a variable after you declare it, but this is not always the case.


2 Answers

You are not returning the value from the recursive call.

Change:

get_link(count, not);

into:

return get_link(count, not);
like image 78
Guffa Avatar answered Sep 26 '22 17:09

Guffa


In the get_link function, you are traversing the nots array using for / in. You should use a regular for loop.

like image 30
HBP Avatar answered Sep 24 '22 17:09

HBP