0

I am trying to produce a variable with one function and use it in another function, and then pass it again to another function from the second function. I have tried placing the variable inside the parenthesis in a multitude of ways but i cannot seem to get the variable to alert in the final function. JSfiddle

function randomNumber(randomstring) {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 16;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    //document.randform.randomfield.value = randomstring;
     // alert(randomstring);
}

$("#btn").click( function() {
        randomNumber(); 
        //alert(randomstring);
        update();
});

function update() {
    alert(randomstring);
}

I have gone through several documents on Scope but i cannot see what i am not quite doing right here.

5 Answers 5

3

A variable declared within a function, with var statement, is visible only within that function.

So, you can return the randomString and pass that around, like this

function randomNumber(randomstring) {
    ...
    ...
    return randomstring;
}

$("#btn").click( function() {
    update(randomNumber());
});

function update(randomstring) {
    alert(randomstring);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is much better practice than solutions that recommend declaring var randomstring in the global namespace. Now your randomNumber() function can be used more than once as opposed to being a one-time-off function that can't be used for any other purpose.
1

You have to declare it in a scope that contains all the functions that use it.

var randomstring; // e.g. here

function randomNumber(randomstring) {
    randomstring = ''; // No var here. Use the existing one, not a new one in local scope

Comments

1

The randomstring variable is with the randomNumber scope. So you have a few options on how to allow the update function to use it.

  1. Make it a Global Variable - This is the worst solution.
  2. Rewrite the Update Function - Call the update function with the randomString as a parameter like so:

    function update(randomstring) { alert(randomstring); }

  3. Create an Object - Wrapping both functions in an object with randomstring being a property of said object. Here is an example:

JSFiddle: http://jsfiddle.net/GFK6B/3/

var obj = {
    randomNumber: function() {
        var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        var string_length = 16;
        var randomstring = '';
        for (var i=0; i<string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum,rnum+1);
        }

        this.randomstring = randomstring
    },
    update: function() {
        alert(this.randomstring)
    }
}

$("#btn").click(function() {
    obj.randomNumber()
    obj.update()
})

Comments

1

define var randomstring = ''; in global name space, not in randomNumber function

JS

var randomstring = '';
function randomNumber() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 16;
    randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
}

$("#btn").click( function() {
        randomNumber();
        update();
});

function update() {
    alert(randomstring);
}

Demo Link

Comments

0

your aren't returning anthing from the first function here is one way to do it by taking randomString as global variable http://jsfiddle.net/venkatjp/GFK6B/2/. return randomstring; in first function and randomstring = randomNumber(randomstring); in click event.

2 Comments

Although other answers are great, this answer is exactly what i required. Thanks!
Unless it is unavoidable, you should not favor this method (for the record, it is very ugly) and go with what I suggested in my answer. If you don't like that, check other answers which at least do this in less ugly manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.