• MrChaney (unregistered) in reply to BillyBob
    BillyBob:
    Remember, Darwin himself said that evolution was just a theory and I'm pretty sure he said it was flawed and he may have also refuted the whole lot on his death bed. All these other biologist who are evolutionists (basically all of them) are wrong, stupid and wasting their time; infact I'd like to know what they do all day - probably burning bibles.

    PS. I'm not a monkey.

    George, don't you have a country to run, rather than pretending to be someone else on a forum?

  • Hellkeepa (unregistered)

    HELLo!

    I'm stunned... So many people ragging on HumptyMoo's code, claiming it's not optimized correctly, and (even worse) that the logic of it is wrong.

    I've got a newsflash for you: HumptyMoo's code (and the follow-ups on it) is the only code that is correct! If you don't believe me, run the following calculation in your language of choice: 2100 % 4

    Meaning: All functions that order mod 4 first, is the same as the guy checking for the last two numbers being "00". They all fail.

    Happy codin'!

  • Hellkeepa (unregistered) in reply to Hellkeepa

    HELLo!

    Seems I spoke a bit too soon:

    Hellkeepa:
    HumptyMoo's code (and the follow-ups on it) is the _only_ code that is correct!
    That statement is not entierly correct, MedO's code (and similar) is correct as well due to the reverse logic on the IF-loops. I'll blame the fact it's before 6am here. :-P

    Incidentally, this just goes to prove that there is a thing such as "too much optimalization". Though, in this case the fault is mine.

    Happy codin'!

  • jc (unregistered)

    AJAX = asynchronous javascript and xylophones

  • Anonymous (unregistered) in reply to vertagano

    or you could just do this...

    function is_leap_year(year){ return !(year% 400)||!(year% 4||!(year% 100)); }

  • lala (unregistered)

    wasn't ajax a football team from holland? :D

  • fastest ;) (unregistered)

    function isLeap(intYear) { return (((!(intYear%400)) || ((!!(intYear%100)) && (!(intYear%4))))); } CAPTCHA: tacos

  • bob (unregistered) in reply to seconddevil

    he was the big hammer dude who smashed in skulls he was a greek hero that got killed the the "trojan war"

  • balalau (unregistered) in reply to Oh come on
    Oh come on:
    Ajax, you know, they sent it out to bring back Flash's body.
  • ovi23 (unregistered) in reply to Oh come on
    Oh come on:
    Ajax, you know, they sent it out to bring back Flash's body.
  • MetalSearGolid (unregistered)

    Isn't AJAX one of the pieces you pick up when you bounce the little red ball?

  • steve (unregistered)

    Any of the following will correctly evaluate true / false for isLeapYear(year).

    !(year % 400) || !!(year % 100) && !(year % 4);   //good enough
    !(year % 4) && !(!(year % 100) && year % 400);    //"optimized"
    (new Date(year, 2, 0)).getDate() == 29; //like, ask the browser
    //this works even if they change when the extra day is inserted
    //leap seconds don't seem to count... Math.round() just in case
    Math.round(((new Date(year, 11, 32))
        - (new Date(year, 0, 1))) / 864e5) != 365;

    Note that Date() takes a month in the range 0-11, not 1-12. Don't ask me why! Someone said to use Date(year, 3, 0). That's the last day in March. Also, Date() assumes that years 0-99 are 19xx. This may or may not be desirable.

    And yes, that last one is more of a joke for WTF effect. Although, it does work... and it should be very robust, as long as year >= 100...

  • steve (unregistered) in reply to steve

    Of course, to make that really robust you'd need to handle some error cases:

    function isLeapYear(year) {
        if (typeof year != "number") year = Number(year);
        if (isNaN(year) || !isFinite(year) || year < 1)
            throw String(year) + "is an unrecognized or invalid year";
        if (year < 100) return !(year % 4);
        return Math.round(((new Date(year, 11, 32))
            - (new Date(year, 0, 1))) / 864e5) != 365;
    }

    Throwing an error is the right thing to do, since (for example) "blue" is not a leap year, but "nineteen fifty-six" is - but the function can't handle that. Better to have it throw an error if it's given a year it doesn't understand.

Leave a comment on “JavaScript Hacks ”

Log In or post as a guest

Replying to comment #:

« Return to Article