• Oh come on (unregistered)

    Ajax, you know, they sent it out to bring back Flash's body.

  • Sauce! (unregistered)

    Wasnt Ajax the war rocket of the bad guys in the Flash Gordon movie?

  • seconddevil (unregistered)

    Ajax was a hero in the illiad, big guy, he got killed...

  • seconddevil (unregistered)

    Its also a brand of washing powder, extremely corrosive

  • Strider (unregistered)
    Comment held for moderation.
  • Paul (unregistered)

    What's even more of a WTF is that the leap year calculation is WRONG. Leap years are years that are divisible by 4, unless they're divisible by 100, unless they're divisible by 400. The year 1900 was not a leap year, 2000 was.

  • Anon (unregistered)

    AJAX (Asynchronous JavaScript and XML) is really starting to piss me off, because I'm starting to hear demands that it be used in totally inappropriate places.

    Places that make it obvious that the person has absolutely no idea what AJAX actually means, let alone what it does.

    I'd have to go searching through my inbox, but I believe I've actually had someone seriously suggest that XML be replaced with AJAX. I'm really not sure where to start on that one.

    And, of course, there were the numerous suggestions that web services be replaced with AJAX, never mind that AJAX is essentially a method of communicating with a web service.

    Plus there are the people who've started to use AJAX to mean "any webpage with JavaScript on it" which is also annoying.

    AJAX isn't really anything new - it's just a new term for an old trick.

    Captcha: scooter (as in Libby?)

  • Paul (unregistered)

    What's even more of a WTF is that the leap year calculation is WRONG. Leap years are years that are divisible by 4, unless they're divisible by 100, unless they're divisible by 400. The year 1900 was not a leap year, 2000 was.

  • Nero (unregistered)

    so, Paul, what's the method for figuring out if a year is a leap year?

  • vertagano (cs)

    Ajax: a brand of dish detergent that everyone should use. It's cheap and gets the job done.

  • Dave (unregistered) in reply to Nero

    Usually your goal isn't to figure out whether a year is a leap year, but to do something like a calendar or date math. If you have to do an explicit leap year calculation for that in Javascript, you are doing something wrong.

    That said, this will tell you whether the current year is a leap year.

    var d = new Date(2008,5,4); d.setMonth(2,0); var isleap = d.getDate()-28;

  • Strider (unregistered) in reply to Nero

    get some number for years

    if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) leap! else dont!

  • Dave (unregistered) in reply to Dave

    Correction whether NEXT year is a leap year.

  • HumptyMoo (unregistered)
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

  • Mixu (unregistered) in reply to Paul
    Paul:
    What's even more of a WTF is that the leap year calculation is WRONG. Leap years are years that are divisible by 4, unless they're divisible by 100, unless they're divisible by 400. The year 1900 was not a leap year, 2000 was.

    I was about to comment on this... leap year isn't as simple as it seems.

  • Will (unregistered)

    That's a perfectly valid means of detecting leap years on the Julian Calendar.

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

    I don't think Ajax KILLED Flash. I mean, you can make websites more interactive using Ajax, but Flash still has its place on the web.

    (Yes, bad joke :) )

  • aikii (cs)

    That's just amazing to see how many coders actually focus on number representation ( as a string ) instead of properties ( modulo, comparison to 0, integer truncation ... ).

    Excusable for teenagers, but not for professionals. Not even non-programmers.

    I remember similar cases in older WTFes, like checking if a number starts with "-".

  • vertagano (cs)
    <script language="javascript"> var year = 2000 var d = new Date() d.setFullYear(year, 1, 29) if(d.getDate() > 28) document.write("Is a leap year.
    ") else document.write("Is not a leap year.
    ") if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) document.write("Is a leap year.
    ") else document.write("Is not a leap year.
    ") </script>
  • Unomi (unregistered)
    if I hear one more person talk about AJAX without being able to tell me what it means, I'll snap.

    Some people snap when you tell them Feyenoord is better than AJAX....

    • Unomi -
  • anonymous coward (unregistered)

    Ajax of spades is my favourite playing card

  • Jamie (unregistered) in reply to HumptyMoo
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

  • mrprogguy (cs) in reply to rbowes
    rbowes:
    Oh come on:
    Ajax, you know, they sent it out to bring back Flash's body.

    I don't think Ajax KILLED Flash. I mean, you can make websites more interactive using Ajax, but Flash still has its place on the web.

    (Yes, bad joke :) )

    I AJAX would kill Flash, though. Flash is just evil, although not 100% evil. It would be 100% evil if the interior scripting language was Perl.

  • Cartroo (unregistered) in reply to Jamie
    Jamie:
    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Well, almost never - it only happens for numbers which aren't divisible by 400, in fact. Still, there can't be many of those, can there?

  • Duston (unregistered) in reply to Nero
    Nero:
    so, Paul, what's the method for figuring out if a year is a leap year?

    You go to the calendar hanging on the wall, flip to the page that says "February" and look for the number "29." If you see it, then it's a leap year. If not, then it's not. Unless of course your calendar was printed wrong. Hmm...so maybe that's not so fool-proof either. That's odd...Come to think of it, my calendar doesn't have any 7's. Better check my phone to see if it has a 5.

  • JB (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Hope this is a joke

  • Strider (unregistered) in reply to Jamie

    Please oh please tell me you're trolling or trying to be funny

  • vertagano (cs) in reply to Jamie
    Jamie:
    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.
    That's because it returns as soon as we have to solution. All years divisible by 400 are leap years. So once we've checked that, we can return true. If it hasn't returned (because it's not divisible by 400), we can check if it's divisible by 100. If it is divisible by 100, then we know that is is NOT a leap year, so we can return false at that point. Otherwise, execution falls through to the next check. Those who dislike multiple exit points (or who use languages that do not allow multiple exit points), would instead be setting a "results" variable to true or false, and on the line return that variable. The variable would be initialized to false to make the default return value false, as in the quoted algorithm.
  • PJH (cs) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.
    WTF. If year isn't exactly divisible by 400, it goes onto the other checks.

  • John (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }
    

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Yeah, the idea is that you never get to the 100 or 4 checks.

    The real WTF around here is usually the comments...

  • NeoMojo (unregistered) in reply to Jamie

    Hush now Jamie, it's ok.

    If the if statement is only one line, you don't need braces.

    Don't get all het up, it'll give you indigestion.

  • Merus (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Um, pretty sure that's by design. If a leap year's any year divisble by 4, unless it's divisible by 100, unless it's divisble by 400, there's no point doing the mod 4 or mod 100 checks if it's divisible by 400.

    captcha: riaa - speaking of people who just don't get it...

  • savar (cs) in reply to Paul
    Paul:
    What's even more of a WTF is that the leap year calculation is WRONG. Leap years are years that are divisible by 4, unless they're divisible by 100, unless they're divisible by 400. The year 1900 was not a leap year, 2000 was.

    AND that logic only applies to the modern calendar, not the Julian calendar. So even if you're clever enough to handle Gregorian dates, you're still going to fail on a wide range of historical dates.

    I try to teach people as a basic philosophy: the world is extremely complex. So complex, in fact, that we fail to see 99% (or more) of the complexity underlying our everyday lives.

    By corollary, if you have to model the world in software, odds are it's going to be utterly complex and you're going to WTF it up in at least a few significant ways. (Think about all the arguments we have here -- among supposedly reasonable developers -- about things like dates, decimal math, hash functions, etc.)

    So don't reinvent the wheel, because you're likely to do it completely ass-backwards and wrong. And any logic you do implement, make sure you explicitly state your assumptions.

  • funthomas (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Hello Jamie, could you check the code with year 2007 or 2008 and rethink your statement? For me, code seems ok. And if you do it with three ifs or with one is just a matter of taste.

  • Whiskey Tango Foxtrot? Over. (cs)

    Come on, everybody knows that Ajax is something you use to clean your dishes and toilets.

  • pshotts (cs)

    Am I the only one who wants to do something about those "var mints" in the uploadTime function? Possibly using a hunting rifle?

  • PS (unregistered)

    FYI in Spanish Ajax is pronounced with the j like an h so it is said like " a hacks" which pretty much sums it up.

  • savar (cs) in reply to Strider
    Strider:
    get some number for years

    if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) leap! else dont!

    I think you win..even though other people had similar logic, you were the only one to "correctly" order the conditions of the predicate from most common to least common.

    75% of years aren't divisible by 4, 99% of years aren't divisible by 100, and 99.75% aren't divisible by 400. Some people check for that itty-bitty .25% slice first when they could be checking for the 25% slice first.

    Then again, most people don't know what short-circuit evaluation is.

  • savar (cs) in reply to funthomas
    funthomas:
    For me, code seems ok. And if you do it with three ifs or with one is just a matter of taste.

    It's not just a matter of taste. You're fundamentally doing different things. You're doing at most 3 comparisons and 3 corresponding conditional branches. The other way to do it is at most 3 comparisons, 2 logical operations, and one conditional branch.

  • Kou Eihou (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    Well, there wouldn't be any point in checking whether if it is divisible by 100 and 4 if the year is already divisible by 400.

  • jesse (unregistered)

    assuming one's piece of code will be out of service in 93 years from now, mod 4 is an adequate check. it's not correct but why bother..

  • Johan (unregistered)

    Isn't be biggest WTF (in the uploading time estimate) to assume that always takes one second to transmit 3500 bytes?

  • vt_mruhlin (cs)

    JavaScript in general pisses me off. My company has a web-based provisioning app that insists on using JavaScript to validate all the form data before you submit it.

    The developers on it set up the forms so that pressing enter in a textbox doesn't actually submit the form. They want me to have to press the submit button with my mouse, because that will "ensure" that I don't try to hack my way around the validation by turning JavaScript off....

    I tell the developer, "umm, I can just look at the source and find the URL it's posting to, send whatever data I want that way." His response? "That's why we do the same validation on the server side".....Why? Why bother?

  • Volmarias (cs) in reply to savar

    Asynchronous Javascript And XML.

    The problem with Javascript is its byzantine methods of doing things, and the fact that the typing is painfully weak. Object Oriented programming (for example) is hacked on in Javascript, and sadly it really shows.

    It's great that it's so easy to learn to use javascript, and I'd consider it an ideal language to teach programming concepts to high school students (delightfully instant feedback when you pair it with the DOM, no need to precompile), except that it's way too forgiving of what are usually mistakes. And, sadly, most of the tutorials and code snippets for javascript were programmed by people who consider things like mirc script the apex of computer science.

  • Dan (unregistered) in reply to Jamie
    Jamie:
    HumptyMoo:
    bool
    isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }

    OK, it's verbose, but it's legible.

    WTF. You've already returned true on the mod 400, and never get to the mod 100 or mod 4 checks.

    that is because 400 is always divisible by 4. It is impossible for 400 to be true and 4 to be false.

  • real_aardvark (cs) in reply to Paul
    Paul:
    What's even more of a WTF is that the leap year calculation is WRONG. Leap years are years that are divisible by 4, unless they're divisible by 100, unless they're divisible by 400. The year 1900 was not a leap year, 2000 was.
    From a Scientific Point Of View (ie one that takes into account Intelligent Design), this is as bad as using Fahrenheit or Centigrade to measure temperature.

    The equivalent to the Kelvin scale here is the Ussher scale, which is calibrated back to an origin of 9AM Oct 3 4004 "BC."

    Ignoring the rounding errors caused by not starting on the boundary between 28 and 29 Feb 4004 BC, you therefore have to take the following into account:

    (a) If the input is before -4004, the output is NaN. (I've always wanted to use that in integer arithmetic.) (b) For all other inputs, adjust by -4004 before applying the div 4, (mod 100 and mod 400) calculations. These are of course subtly different because of the irritating extra 4 that God chose to put in there.

    You can ignore leap-seconds, the precession of the Earth's axis and the slow degeneration of the Earth's orbit for any sensible date in this range because the numbers involved are too small to matter. Which just goes to show how clever Ussher was, and that Intelligent Design is obviously correct.

    And I'll bet they haven't factored that into VB.Net libraries ... just another reason to hate the goddamn language.

  • kirchhoff (cs) in reply to Volmarias
    Volmarias:
    Asynchronous Javascript And XML.

    The problem with Javascript is its byzantine methods of doing things, and the fact that the typing is painfully weak. Object Oriented programming (for example) is hacked on in Javascript, and sadly it really shows.

    There is nothing hacked onto it; ECMAScript has no notion of being a "real" OOP language.

    You have to use programming conventions about object factories and closures to emulate things you take for granted, like static/class methods and properties, mixins and private methods/data. But you can simulate almost anything provided you adhere to some generally accepted practices.

    For the user of objects, it's pretty clear cut. It's harder for the class writer than say, Java or C#, or even perl.

    But it's lovely in that: hey, everything's a hash. Makes manipulating and displaying text data trivial, and that's what it should be used for. (Thank god for Perl REs in JS 1.3)

  • gc (unregistered) in reply to real_aardvark

    I don't know javascript, so don't know if this is available, but couldn't you just do:

    // Feb 1, yyyy GregorianCalendar gc = new GregorianCalendar(yyyy,1,1); if (gc.isLeapYear()) { // leap } else { // not }

    or am I just missing the point?

  • MedO (unregistered)

    bool isLeapYear(int year) { if (year % 400 == 0) return true;

    if (year % 100 == 0)
        return false;
    
    if (year % 4 == 0)
        return true;
    
    return false;
    

    }

    Not really optimized, though... this code will do three comparsions for most years. Checking in reverse order most years require only 1 comparsion, or 2 in some cases:

    bool isLeapYear(int year)
    {
        if (year % 4 != 0)
            return false;
    
        if (year % 100 != 0)
            return true;
    
        if (year % 400 != 0)
            return false;
    
        return true;
    }
    

    I love pointless optimisations :-)

  • steve (unregistered) in reply to jesse
    jesse:
    assuming one's piece of code will be out of service in 93 years from now, mod 4 is an adequate check. it's not correct but why bother..

    Another WTF. Without knowing (and understanding) how the software will be used, you can't make this assumption. Any date computation prior to Mar 1, 1900 will fail.

Leave a comment on “JavaScript Hacks ”

Log In or post as a guest

Replying to comment #:

« Return to Article