- 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
parseInt('09', 10) will work, second param is the base. The real WTF is Protoype. And the calendar plugin. And JavaScript.
Admin
0106 0101 0111 0114
Admin
The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.
Admin
It's a known feature, you must use parseInt(String,radix), where radix = 10 to tell it's decimal (2 for binary, 16 for hexa, etc.). When the value is not precised, javascript tries to guess, and since the prefix for octal numbers is "0", "01" to "09" are transformed into respective octal values 1 to 7 then 0 and 1, hence the strange results for august (08 octal = 0 decimal) and september (09 octal = 1 decimal). I don't see a WTF here, every javascript coder must have met this strange behavior.
Admin
Classic example of "it's not a bug; it's a feature". That's why it has a second parameter. You could only say the implentation in the browser needs better guessing functionality.
Admin
I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.
Admin
Everyone who met the strange UK alcohol restrictions, only recently relaxed, which stated that you couldn't buy a drink in a pub after 11pm still experienced a WTF moment - especially visitors to the UK who seemed to find it especially amusing.
Admin
Admin
Admin
You want to see the Real WTF, try doing the following in a JavaScript console.
See, parseInt("085") will decide it's 0 because it can't match an octal number.
The JavaScript language ITSELF will merrily say "oops, that's really a decimal" and treat it as such.
Of course, the behavior of parseInt(str) is really wrong anyway, since it HAS an error return: NaN. If the given value cannot be parsed, it SHOULD return NaN!
Admin
Hm, I thought every sane javascript developer has become extinct. I must be wrong or the world is twitching apart.
Admin
Or MS Silverlight. I actually like XAML.
Admin
Just because you don't know the particulars of the language doesn't make it a WTF.
Some people thing the ?: operator in C/C++/C# is quirky. Does that make it a WTF?
If we classify any exception to the standard as a WTF, then 80% of the English language is guilty.
The real WTF is that the writer thinks he knows Javascript.
Admin
I believe Javascript will return NaN. But really, I wish browsers would just fail things like Javascript and HTML when the developers can't be arsed to actually implement them right. This would have stopped IE in its tracks long ago, but no, we had to be "kind".
It would also make it easier to find errors instead of having to parse through everything yourself.
Admin
This has to be one of my biggest pet peeves with current programming languages in general. Why the HELL is there any constant which is inferred to be octal? I have never used an octal constant in my life. Decimal, sure! Hex, all the time. But octal - NEVER, EVER, EVER, NOT EVEN ONCE!
PS: I note this is different from Java Integer.parseInt() which treats a String as decimal unless a specific radix is supplied - though Java, too, does treat literals as octal if they have a lead 0.
Admin
What's the quirk? It works just as I would expect it to in Java/D; is it different in C/C++/C#?
Admin
Because Perl did the same thing!
The only thing octal has ever been good for is file permissions. Octal should be shot in the head, driven over a cliff, shot in the head again, and then incinerated.
Admin
var date_bits = element.value.match(/^(\d{4})-0*(\d{1,2})-0*(\d{1,2})$/);
Admin
Agreed.
And don't even get me started on 09/01/2008.
Admin
I had a similar WTF with jsript date method calls. Apparently jscript date objects have the "feature" of resetting the day of month to 1 if the day is set prior to the month:
var month = 3 var date = 31 var year = 2008
var fromDate = new Date; fromDate.setDate(date); fromDate.setMonth(month - 1); // January = 0 fromDate.setFullYear(year);
WScript.Echo(fromDate);
Admin
And 7-segment displays, but if you're using JavaScript to control one of those then you might want to rethink your design...
Admin
I'm pretty sure you're confusing FLEX with AIR.
Admin
Don't blame Javascript, blame atoi.
I've seen this same behaviour elsewhere, it's because it's dropping down to C's standard library.
Admin
But it can parse it. Well, part of it.
parseInt("9abc") == 9
parseInt("05585") == 45 == 055
Admin
"Apparently jscript date objects have the "feature" of resetting the day of month to 1"
I've never used jscript, so i'm just guessing here, but could it be that the date object defaults to the current month and year if you haven't yet put them in when setting the date part? So if you start off by setting the date to 31, and the current month has 30 days, it silently fails and sets the date to 1 instead. That would still suck, but also make some sense.
In the interests of science, this might be interesting to run:
Admin
There's a similar feature in Windows Vista (maybe XP too). Edit an MP3's tags using the summary page of the file's properties. Now enter track numbers 01, 02, etc. It will reject your attempt to save track 08 and 09, but 10 will be fine.
Admin
If this a WTF, then all of the people who try to float two
Anyone who knows anything about HTML knows that if you want two things side-by-side you use the tag, which doesn't include a break before and after it!
Admin
@steve: the problem here is that the default value of a javascript date object is the current date. First, you are setting the day of the month to 31. But wait! We are in April, which only has 30 days! So, it tries to set April 31th, and we end up with May 1st (the day after April 30th). Then you set the month to March, we end up with March 1st.
Instead, set the full date at once: var fromDate = new Date(year, month-1, date);
Admin
My personal pet peeve is that most languages still don't support binary constants.
Admin
Anyone who says they never use octal and octal has no use probably just don't recognize that they are using it or that some utilities assume you are passing octal on the command line.
0755 anyone?
And BTW, if JavaScript really has a NaN representation for int, I think that is a WTF.
Admin
Example: Will set the date to January 365, 2008 it'll make the date January 30, 2009.
Admin
Admin
Beware of Allen Bradley / Rockwell PLC addressing.
Admin
Some of us don't mind typing "a=rx,u+w". It's clearer.
Admin
Hardly, because JavaScript has no 'int'. Only 'number' which can be of course be a NaN.
Mike5
Admin
I also vote for "no WTF here". System working as designed, which is exactly as correct as the way the OP wanted it to work. If you're going to support octal at all, you have to do it right. And if you're going to provide a way to have the system guess which kind of number you're parsing, you need to do so consistently. So they made up some rules, documented them (right?), and implemented it that way.
The reason it doesn't return NaN is because parseInt is trying to be permissive. Try this:
The first one returns NaN, while the second returns 10. The documented behavior (there we go with that again!) is to try really hard to find a number, then quit if we hit something that's not part of the number. So what the parser encountered is 0 (I'm octal!), 9 (I'm not a number in the current set, so stop parsing), and it returned 0.
That's why they say "I'll treat it as octal if it starts with a 0 and stop parsing once I hit an 8 or 9" instead of "I'll treat it as octal if it starts with 0 and there are no 8s or 9s (otherwise, decimal)".
If there is a WTF, it's using Javascript functions without reading the documentation about what they do.
Admin
Admin
Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)
You don't use QWERTY? Respect.
Admin
It's some kind of prophecy! RUN FOR YOUR LIVES! :(
Admin
alert("077" - 0 == 7); (true) alert("09"-0 == 9); (true)
Well at least on Firefox.
Admin
Have you ever considered using 4-digit year strings? I mean, what is this, Y2K8 or something? :P
Admin
...or maybe he uses the numeric pad ;)
Admin
What about the numeric pad? :)
Admin
No, 100%. The plus sign on the numeric keypad is a single keystroke.
You don't use the numeric keypad? Respect.
Admin
(On the other hand I need shift+0 to type '=')
Admin
Spot the false assumption. ;)
Admin
This is a Javascript rite of passage. It bites everyone once, and then you never forget.
Admin
Admin
Quickly, now: is 01-09-2008 the first of September--or is it the ninth of January (which is how we'd read it where I come from)?
Admin