Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possile to put conditions on functions while calling them 2

Status
Not open for further replies.

n4s

Programmer
Nov 20, 2002
11
NZ
Can any one tell me, is it possible to put conditions on functions while calling them from another function ?

Ex:-
function function1()
{
function2();
if function2() return value is true then
function3();
else
function4();
if function4() return value is true then
function5();
else exit

}


Plese give me the syntax or example for the above.

Thank you,
SAHI.







 
Here's a quick sample:
Code:
<script>
<!--
onload = init;

function init() {
	if (init2() == false) {
		init3();
	} else {
		init4();
	}
}

function init2() {
	if ("me" == "you") {
		ret = true;
	} else {
		ret = false;
	}
return ret;
}

function init3() {
	alert("me doesn't equal you :\)");
}

function init4() {
	alert("me equals you..?");
}
//-->
</script>
 
Hi Supra

No, not at all! I am != you, as me is a pupil in Javascript but you are not it seems. So am I == U ?

I am just kidding.

I'll try with your given example, thank you very much for your reply.
 
SAHI,

note that a common mistake to make when taking the route Supra suggests is to make a string out of 'false'. For example:

Code:
function init() {
    if (init2() == 'false') {
        init3();
    } else {
        init4();
    }
}

This won't work if the function returns false, but WILL work if it returns 'false' (note the quotes).

What Supra suggests is correct, since there are no quotes. Just a heads-up to watch out.

Since they are boolean values being passed, you could also write:

Code:
function init() {
    if ([b]!init2()[/b]) {
        init3();
    } else {
        init4();
    }
}

--Dave, responding only because I like to hear myself "talk"!
 
Dave
Thanks for giving a valuable suggestion.


-Sahi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top