Page 3 of 6 FirstFirst 12345 ... LastLast
Results 21 to 30 of 58

Thread: Funny Code

  1. #21
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Arie, I'd guess that's because >= is strictly a mathematical operator, so it can't/won't handle non-numerical values properly.



    Continuing on the comments discussion, it's always amusing to read a creatively commented script. At the top of my list I think are IPB and SMF forums' code, always complete with very strange sounding comments... something like "//put the dog in the doghouse" or equally strange things.
    (Then as I begin to edit things, I begin to make complaints like "//because this is broken!!")
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. The Following User Says Thank You to djr33 For This Useful Post:

    molendijk (08-10-2012)

  3. #22
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Makes perfect sense.

    Code:
    >=
    is a mathematical comparison operator. It type converts the items to be compared to numbers. If one or both of the items being compared is NaN, well NaN is equal to nothing, not even to itself.

    When you use == || >, in javascript == is a type non-specific comparison operator, so anything that is roughly equivalent will match.

    Code:
    '1' == 1
    is true. In your example it's:

    Code:
    undefined == undefined
    As it so happens, I believe that:

    Code:
    null == undefined
    and definitely that:

    Code:
    0 == false
    those sort of comparisons are also true.

    If you want a type specific comparison, use ===

    With that undefined === undefined is still true, but those others are not. With type specific comparison you can always first covert both to a chosen type and then see if they're still equal.

    I could go on and on with this sort of thing. But I think you get the point.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    molendijk (08-10-2012)

  5. #23
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

  6. #24
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Quote Originally Posted by molendijk View Post
    I have some code that may not be funny, but it surely is funnily strange. It's about how javascript handles and/or statements. An and/or statement is only false when both members of the statement are false (as opposed to either...or statements that are only false when the members are both true or both false).
    So this:
    Premise: x1=x2
    Conclusion: (since x1=x2), 'x1 is bigger than or equal to x2'

    is a true statement.

    Let's convert that to javascript. If we put:
    Code:
    <script>
    var x1, x2;
    x1=x2;
    if(x1==x2 || x1>x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
    </script>
    we get the expected result.
    But if we put
    Code:
    <script>
    var x1, x2;
    x1=x2;
    if(x1>=x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or)')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
    </script>
    we incorrectly get a non-expected result.
    So using '||' gives the expected outcome, whereas '>=' makes trouble.
    What makes things even more weird is that '>=' stops annoying us when we assign concrete values to x1 and x2, as in:
    Code:
    <script>
    var x1=1, x2=1;
    x1=x2;
    if(x1>=x2){alert('CORRECT.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or)')} else {alert('WRONG.\nWe have postulated that x1 is equal to x2, so x1 is equal to or bigger than x2 (inclusive or).\nWhy am I getting this "else" alert that says I`m wrong?')}
    </script>
    What is going on here?
    ===
    Arie.


    You lost me at javascript

  7. #25
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by jscheuer1 View Post
    Makes perfect sense.
    Code:
    >=
    is a mathematical comparison operator. It type converts the items to be compared to numbers. If one or both of the items being compared is NaN, well NaN is equal to nothing, not even to itself.
    Yes, I got it. The same holds for
    Code:
    !=
    . This explains why
    Code:
    <script>
    if(NaN==NaN){alert('CORRECT')} else {alert('WRONG')}
    if(NaN!=NaN){alert('CORRECT')} else {alert('WRONG')}
    </script>
    gives 'WRONG' first, then 'CORRECT'.
    Thanks for the enlightment.
    Arie.

  8. #26
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Wow, when did this thread become about actuall code? Just stumbled on some gems:
    Code:
    // I am not sure if we need this, but too scared to delete. 
    
    // I am not responsible of this code.
    // They made me write it, against my will.
    
    //Dear future me. Please forgive me. 
    //I can't even begin to express how sorry I am. 
    
    options.BatchSize = 300; //Madness? THIS IS SPARTA!
    
    // I have to find a better job
    
    // hack for ie browser (assuming that ie is a browser)
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  9. #27
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Code:
    // this should work. Hold my beer will you?
    Code:
    // 
    // Dear maintainer:
    // 
    // Once you are done trying to 'optimize' this routine,
    // and have realized what a terrible mistake that was,
    // please increment the following counter as a warning
    // to the next guy:
    // 
    // total_hours_wasted_here = 39
    //
    Code:
    // When I wrote this, only God and I understood what I was doing
    // Now, God only knows
    Code:
               
     // MSXML is... special.             
    // (Yes, we really do need fifty lines of code for IE and *one* for everything else)
    Code:
    // this code makes the bosses cd tray open at random intervals for no reason. As apparently I dont need to go the bank today.
    Code:
    aComment = 'this is not aComment' # this is aComment
    ---------------------------------------------------------------------------

    Wow, when did this thread become about actuall code?
    lol
    Last edited by keyboard; 08-11-2012 at 01:08 AM.

  10. #28
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by keyboard1333 View Post
    Code:
    /* Seriously, people will die if you change this */
    Thanks for reading my post keyboard, I've already done that one.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  11. #29
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

  12. #30
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by bernie1227 View Post
    Code:
    /* seriously, don't touch this or people will die */
    Deny It all you want, doesn't make it false
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

Bookmarks

Posting Permissions

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