www.webdeveloper.com
Results 1 to 3 of 3

Thread: code explanation

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    code explanation

    Could any one tell me why the result of below code is: 3,5,1,6,2,4


    var myArray = [1,2,3,4,5,6];
    myArray.sort( function(x, y) {
    if (x % 2)
    return -1;
    if (x % 2 == 0)
    return 1;
    }
    );
    alert(myArray);

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,265
    This sort place even number after all others... But the result is for me 5, 3, 1, 2, 4, 6.

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by gscw View Post
    Could any one tell me why the result of below code is: 3,5,1,6,2,4
    What result do you expect?
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles