0

I am having two functions in my JavaScript code, and I want to perform some action on the basis of value returned by other function.

Here my code :-

function test1(){
    var radio = document.getElementByName('sample');
    for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = return test1();
    // some code and work according to the value in returnValue
}

Well I know return in function test2 is not correct. so what can I do here NOW????


Edit

Here what I am Trying to do... but it do not seem to be working http://jsfiddle.net/U6RjY/7/

EDIT2 ----- The corrected and working fiddle... http://jsfiddle.net/U6RjY/9/ Thanks to all :)

1 Answer 1

3
function test1(){
    var radio = document.getElementByName('sample');
     for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = test1();
    // some code and work according to the value in returnValue
}

Just remove the return.

You will notice however that in function test1, it will return the value on the first loop. So, it will stop executing.

Live example: http://jsfiddle.net/U6RjY/

Sign up to request clarification or add additional context in comments.

4 Comments

Oh I got it, Thanks, it's working fine on jsfiddle... may be I am out of luck today..my code is having some problem.
What I am doing is : - jsfiddle.net/U6RjY/7 now why this is not working??
It is not working.. in my code it is still logging undefined
Thanks.. there was another problem because of which it was going undefined My code is working fine now.. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.