By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Help Determining Result of Function (true/false)

P: 2
Hi Guys. I'm hoping someone here knows how to do this.

I have a function, let's say:
Expand|Select|Wrap|Line Numbers
  1. function dosomert()
  2. {
  3. doing some stuff;
  4. return valid;
  5. }
  6.  
Now, as you can see, if all is run then it returns valid. But how do I use an if statement to find out what this particular function has returned?

Like:

Expand|Select|Wrap|Line Numbers
  1. function checkvalid(){
  2. if (function dosomert.returned='valid'){
  3. do something else;}
  4. }
  5.  
Any ideas?

Thanks.
Aug 10 '08 #1
Share this Question
Share on Google+
3 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. // Create a function to compare something
  2. function compareThings(thing1, thing2) {
  3.   if(thing1 == thing2) {
  4.     return true;
  5.   }
  6.   else {
  7.     return false;
  8.   }
  9. }
  10.  
  11. // Check the thing and print results
  12. var result = compareThings(1, 2);
  13.  
  14. if(result == true) {
  15.   document.write("They match!");
  16. }
  17. else {
  18.   document.write("They don't match!");
  19. }
  20.  
Is this what you are talking about?
Aug 10 '08 #2

P: 2
Hi.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. // Create a function to compare something
  2. function compareThings(thing1, thing2) {
  3.   if(thing1 == thing2) {
  4.     return true;
  5.   }
  6.   else {
  7.     return false;
  8.   }
  9. }
  10.  
  11. // Check the thing and print results
  12. var result = compareThings(1, 2);
  13.  
  14. if(result == true) {
  15.   document.write("They match!");
  16. }
  17. else {
  18.   document.write("They don't match!");
  19. }
  20.  
Is this what you are talking about?
OMG, that is exactly what I was talking about. My mistake was not putting the result of the function in a variable. That did the trick.

Thank you so much for your time!
Aug 10 '08 #3

Atli
Expert 5K+
P: 5,058
OMG, that is exactly what I was talking about. My mistake was not putting the result of the function in a variable. That did the trick.

Thank you so much for your time!
Actually, putting it in a variable isn't really needed either. Although, if you plan on using the returned value more than once, it's best to put it into a variable.

You could just do:
Expand|Select|Wrap|Line Numbers
  1. if( compareThings(1, 2) ) {
  2.   document.write("Match!");
  3. } else {
  4.   document.write("No match");
  5. }
  6.  
The compiler will basically replace the function call with the value it returns.
Aug 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.