- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
George, don't you have a country to run, rather than pretending to be someone else on a forum?
Admin
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'!
Admin
HELLo!
Seems I spoke a bit too soon:
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. :-PIncidentally, 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'!
Admin
AJAX = asynchronous javascript and xylophones
Admin
or you could just do this...
function is_leap_year(year){ return !(year% 400)||!(year% 4||!(year% 100)); }
Admin
wasn't ajax a football team from holland? :D
Admin
function isLeap(intYear) { return (((!(intYear%400)) || ((!!(intYear%100)) && (!(intYear%4))))); } CAPTCHA: tacos
Admin
he was the big hammer dude who smashed in skulls he was a greek hero that got killed the the "trojan war"
Admin
Admin
Admin
Isn't AJAX one of the pieces you pick up when you bounce the little red ball?
Admin
Any of the following will correctly evaluate true / false for isLeapYear(year).
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...
Admin
Of course, to make that really robust you'd need to handle some error cases:
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.