Help Determining Result of Function (true/false)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DamienRoche
    New Member
    • Aug 2008
    • 2

    Help Determining Result of Function (true/false)

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

    I have a function, let's say:
    Code:
    function dosomert()
    {
    doing some stuff;
    return valid;
    }
    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:

    Code:
    function checkvalid(){
    if (function dosomert.returned='valid'){
    do something else;}
    }
    Any ideas?

    Thanks.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Consider this:
    [code=javascript]
    // Create a function to compare something
    function compareThings(t hing1, thing2) {
    if(thing1 == thing2) {
    return true;
    }
    else {
    return false;
    }
    }

    // Check the thing and print results
    var result = compareThings(1 , 2);

    if(result == true) {
    document.write( "They match!");
    }
    else {
    document.write( "They don't match!");
    }
    [/code]
    Is this what you are talking about?

    Comment

    • DamienRoche
      New Member
      • Aug 2008
      • 2

      #3
      Originally posted by Atli
      Hi.

      Consider this:
      [code=javascript]
      // Create a function to compare something
      function compareThings(t hing1, thing2) {
      if(thing1 == thing2) {
      return true;
      }
      else {
      return false;
      }
      }

      // Check the thing and print results
      var result = compareThings(1 , 2);

      if(result == true) {
      document.write( "They match!");
      }
      else {
      document.write( "They don't match!");
      }
      [/code]
      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!

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by DamienRoche
        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:
        [code=javascript]
        if( compareThings(1 , 2) ) {
        document.write( "Match!");
        } else {
        document.write( "No match");
        }
        [/code]
        The compiler will basically replace the function call with the value it returns.

        Comment

        Working...