Can you explain this code¿?

Discussion in 'Web Design, HTML And CSS' started by raulcorrales, Mar 23, 2011.

  1. raulcorrales

    raulcorrales New Member

    Joined:
    Mar 22, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi to all the users.

    I have find this code in this forum:


    Code:
    function MyFunction(a,b)
    {   
        return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));
    }

    I've used for my program and it works, does what I want, but do not understand their ways.

    Can you explain the function¿?

    Thanks
     
  2. Cristi1213

    Cristi1213 New Member

    Joined:
    Jan 17, 2011
    Messages:
    6
    Likes Received:
    1
    Trophy Points:
    0
    The syntax is something like this: condition ? trueResult : falseResult;
    First the condition is verified. If it is true than trueResult is returned, else falseResult is returned. In your example, the second one is evaluated first. If a1 is bigger than b1 1 is returned, else 0, than the second one is evaluated, -1 if a1 is smaller than b1, the previous result otherwise. That is -1 in case if a1<b1, 1 if a1>b1, 0 of they are equal.
     
  3. pyasakumar

    pyasakumar New Member

    Joined:
    Apr 12, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Seo
    Location:
    Delhi
    Home Page:
    http://www.singaporetourpackage.com
    Hello dear
    I think also Its a condition. True and False Result
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice