- 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
Admin
This is why Strongly Typed languages like C# and Java (as in real Java not JavaScript) rule! In C# you would have to explicitly define your '09' as an 'oct' type and the method parseInt would have to be overloaded to except an 'oct' type argument.
How can people think JavaScript is a cool language!?
Admin
Admin
The only WTF here is that Charles Capps and the original programmer of the date widget didn't know that.
Not really a WTF.
Admin
Great, so instead of building one website, we now have to build a program that works on Windows, another that works on Mac, then another that works on Linux, and then there's the mobile/PDA/iPhone versions. Just what I need, more work!
Admin
Admin
Admin
Let's all use ActiveX controls.
Admin
I wouldn't blame Javascript... I would blame the browser manufacturer.
Admin
JS didn't have exceptions when the Date object was implemented and standardized, though its Date object was largely copied from Java -- recognized now as a mistake, but seemed like a reasonable plan at the time.
In Firefox, at least, we permit the "illegal" octal of 09 as a literal, because octal/decimal literals don't have a way to override the radix used to parse them, and web compatibility demands it. For parseInt, you can specify the radix, and should.
Admin
Admin
No, it shouldn't. parseInt()'s documentation states that it attempts to parse as much of an integer out of a string as possible. so "20th" returns 20, "0th" returns 0, and since '09' is detected as an octal number, and 9 isn't a digit in the octal system, '09' returns 0.
When you call parseInt without specifying a radix, it applies some simple hueristics to guess what type of number it is. Simply put, 0x starts a hex number, 0 starts an octal number, and 1-9 start a decimal number. If it tried to be smarter, it would become more unpredictable.
The developer just needs to specify the radix. That's TRWTF here.
Admin
Admin
Not to forget the Americanisation, sorry, -ization of dates: 09-01-2008 (mm-dd-yyyy)
Lucky SQL 2008 is handling it...oh no, wait...it's bringing out more different datetime data types...
Admin
Admin
If you are serious when saying this, then you are among the people who made the web such a pain in the ass to build for. Are you perhaps working on the IE team?
is not a block-level element, which
Block-level elements contain other elements, which could be blocks or non-blocks. Inline (which is) is only designed to contain text and/or other inline elements. When you want to position two things side by side, it is usually not text in there.
Note the difference between a paragraph and text here.
is a block level element which may contain text, , , etc. A
may not contain other blocks, though, so placing an image ([image]) inside a paragraph is stupid and non-standard. An image is not text, and it should be placed outside the paragraph.
So if you're using elements to float contents side by side, you may not use images, lists, tables, forms, dividers, horizontal rules or any other block element in them. What is it good for, then? Well, you could do this:
I don't see the point of that, though. This could be accomplished better by placing paragraphs floated like that.
Yes, you may override the display type of an element. A span may display and behave like a block when you use the property "display" and set it to "block", but this does not change the schematics, and CSS should be presentational ONLY.
Please... Use standards, and try to at least understand them before you do. "Floating s" was even stupider than the original article here. :-(
Admin
This isn't a bug, and it's not even an undocumented feature. It's part of the spec. So if you ran into this, it's your own fault.
If you want parseInt to assume base 10 constantly, do something like:
var oldParseInt = parseInt; parseInt = function(theNum){ oldParseInt(theNum,10);}
Admin
O_o WTF ?!??!
Admin
This post tries to explain why JavaScript behaves the way it does:
http://blogs.msdn.com/ericlippert/archive/2003/10/06/53150.aspx
[C# (and lots of other languages) is designed for professional developers, not hobbyists and departmental web developers who just want to get "glue code" written as rapidly as possible.
That's why JScript's attitude towards error cases is not "die at the first sign of trouble" but rather "muddle along as best you can". Assign to an undeclared variable? Declare it dynamically. Forget a semicolon? Insert it. Try to create a date for the 31st of September? Move it to October 1st. Divide by zero? Return a NaN . Reference an element beyond the end of an array? Grow the array. All these things that would be compile time or runtime errors in other languages are just handled for you in JScript, for better or for worse.]
BTW, when Eric says "JScript", he means "ECMAScript ECMA-262" http://www.ecma-international.org/publications/standards/Ecma-262.htm which was basically built from what Netscape originally released as JavaScript and Microsoft (and others) copied to try to be compatible with the original JavaScript 1.0. If you view the above behaviour as "problems", then they aren't the result of anything Microsoft did, other than copying the original JavaScript implementation.
Anyway, the parseInt() behaviour is clearly documented in section 15.1.2.2 of ECMA-262, the new Date() behaviour is less clearly documented in section 15.9.3.1.
In closing, I'd like to post some quotes from Richard Cornford, author of http://www.litotes.demon.co.uk/js_info/private_static.html and http://www.jibbering.com/faq/faq_notes/closures.html (and numerous other documents) which demonstrate that JavaScript is a rich language full of potential untapped by most developers.
[...you might want to know that cross-browser scripting is one of the hardest programming tasks that there is, because such scripts cannot know in advance which browser environment they will be attempting to execute in and must be designed to exhibit planned behaviour in the face of all of the possible permutations. It is such a difficult task that few even aspire to attempt it, let alone successfully achieve it.
Unfortunately browser scripting is superficially easy to do. There is no requirement for special authoring software, a text editor and a web browser is enough and most people have those as pare of their OS. The language itself is relatively simple, not always appreciated for its subtleties and capabilities but not difficult to assemble a sequence of statements and achieve expected results by executing them in a browser.]
[...It is a "toy" language until it bites back and then it is broken language because it didn't turn out to be the toy that the misconception made it.
Javascript's loose typing and dynamic nature, from the perspective of a class-based language programmer, may seem to be concessions made for the benefit of undisciplined amateur programmes. In reality they are powerful features ideally suited to programming for the quicksand environments of web browsers. Much harder to properly exploit than they at first seem, and requiring at least as much discipline on the part of the programmer to keep track of. In fact frequently a major stumbling block for amateur scripters rather than being a way of avoiding exposing them to issues.]
Admin
Admin
Per-OS?? Let me think -- FAIL. You haven't a clue of the whole point behind web browsers.
Besides, you already have everything you've asked for above -- it's called C++. You might want to look it up.
For the rest of the folks here, remember the first integer in any numeric base you care to think of starts at zero. 0 goes to 0, 1 goes to 1, ..., 7 goes to 7, and 8 goes to 0, 9 goes to 1, 10 goes to 2, etc.
Not only was the article wrong about its octal mapping, so too were a surprising number of comments offering "corrections."
Admin
Either I missed your point here or you're completely wrong. There is no "mapping" in octal. 8 and 9 are invalid octal digits, period. And by the way, 10oct is 8dec...
Admin
"0 goes to 0, 1 goes to 1, ..., 7 goes to 7, and 8 goes to 0, 9 goes to 1, 10 goes to 2, etc. "
8 goes to 0, 9 goes to 1? WTF?!
Admin
Well, this behaviour is the correct and documented behaviour for ParseInt. Every javascript developer should use some verification tool like JSlint, which would issue a warning when ParseInt is called without using the radix parameter, because it's dangerous (and I repeat, perfectly documented) to do so. The WTF here is not javascript, but the developer who has written these lousy code.
Admin
That's because THE DATES KEEP CHANGING!! As soon as a computer is able to handle a date, it CHANGES! And it never changes back to one that's already happened, that'd be too easy!
Admin
The read WTF is that we're 75 comments in and no one has actually posted the right answer to this problem
parseInt("08") // => 0 parseInt("10foo") // => 10 parseInt("foo") // => NaN
Number("08") // => 8 parseInt("10foo") // => NaN parseInt("foo") // => NaN
Simple
Admin
To Lawrence-
It's legacy from C. It was originally developed on a PDP machine which had 6-bit characters.
What's weird is that C uses 0x prefix for hexadecimal constants, but 0 for octal constants. Had they been consistent in their definitions none of this would have happened.
Admin
Yes, I'm the real WTF.
parseInt("08") // => 0 parseInt("10foo") // => 10 parseInt("foo") // => NaN
Number("08") // => 8 Number("10foo") // => NaN Number("foo") // => NaN
Admin
Now that I think a bit more, the PDPs had 6-bit characters in some places and 7-bit in others.
Admin
Just (mis)use the loosely typed variables of JavaScript and thus avoid parseInt on a string you already know is a digit. Simply subtract 0 from the "string" and you get the number. Problem solved :D
Admin
parseInt is almost never what you want. Number("09") evaluates to 9.
Admin
Look, as with any language, you have to know how it behaves in the environment that its used.
In JavaScript, months are 0-indexed.
So, var d = new Date(2008,0,365); would be January.
It is a bit egocentric to think that just because the language doesn't behave how you expect it to behave, that it's wrong.
Admin
In other words, the code: flag ? a=1:a=2; in C is the same as: (flag ? a=1:a) = 2; and in C++ is the same as: flag ? (a=1):(a=2);
C and C++ aren't as compatible as most people think.
Admin
That's a well known bug in JavaScript. Just use parseFloat().
Admin
prompt>cat tst_atoi.c #include <stdio.h> #include <stdlib.h>
void main() { char *bla = "09";
} prompt>cc tst_atoi.c prompt>./a.out 9
atoi converts for decimal base, if you want to force a base then there are other functions (eg. strtol and kin).
P.S. Yes, I used "void main()" rather than "int main(int argc, char **argv, char **env)", or even "int main(int argc, char *argv[], char *env[])"; want to fight?
Admin
0120 0125 0116 0124
Admin
Regardless, code like that is why I have seen coding standards disallow use of the ? operator. I would say a sane use of the operator performing the same function would be:
a = flag ? 1 : 2;
and if you really want to visually clue in the clueless:
a = (flag ? 1 : 2);
Of course, if you did:
flag ? (a = 1) : (b = 1);
then it appears that you want two different behaviors based on flag and I would suggest an if better conveys that message.
Admin
IMO, Javascript is an awful language and I'm not talking about the syntax. It incorporates one of the worst practices possible which is to bury errors. This parseInt example is a good illustration. Sure it is powerful, much like heroin. A tiny bit of heroin might not kill you and might even feel good at first, but it doesn't take much to make it lethal.
Admin
scroll, scroll, scroll Wow, only about 75% of the comments are people complaining about me forgetting about the radix paramater. I originally guessed 90%. Damn.
Admin
And Halloween is Christmas... 31oct == 25dec ;)
Admin
Supporting octal (especially in JavaScript) is a WTF which I'm sure is only in there because other languages did that too. And why do they do that? Because languages for ancient obsolete hardware did that. Supporting octal in and of itself isn't stupid, but inferring octal from a leading zero is insanely stupid and only justified by the miniscule memory and processing powers of those old systems.
There is just zero justification for this any more. I know so many people who have been tripped up by this and I bet nearly everyone who would defend 0011 parsing to 9 thinks the horrible, horrible imposition of having to type a 0x for 0x0A is just fine - and it is. Octal should be explicit and parsing an integer should always be base 10 unless overridden. Even documented bad behavior is still bad behavior.
Admin
Um, wtf? Lawrence said "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? Current programming languages in general have octal constants because Perl does?
However, Perl does act pretty much like JavaScript in that strings containing numbers, used as if they were numbers, are interpreted in base 10. It even has a function that converts a string to a number, obeying "0x" (for hex) or "0b" (for binary) prefixes, and otherwise using a default radix. It's called...
oct
Admin
For all the poor souls that have to suffer programming in JavaScript, here are obligatory videos.
Basics (for people who think they figured out how JS works): http://video.yahoo.com/watch/111593 http://video.yahoo.com/watch/111594 http://video.yahoo.com/watch/111595 http://video.yahoo.com/watch/111596 (really, watch it. it's worth lots of laughs)
And advanced: http://video.yahoo.com/watch/111585 http://video.yahoo.com/watch/111586 http://video.yahoo.com/watch/111587
Admin
A real wtf would be a mature language with a standard library function to convert strings to numbers coming up with a new specification that switched it to interpreting a leading 0 as indicating octal.
Can anyone name the one-character language I mean?
Admin
Pedantic mathematician mode=on
Nope, sorry. You're confusing modulo arithmetic with base number representation. One is a system of setting numbers equivalent to each other, the other is a way of writing those numbers.
In the first, yes, 8 mod 8 is 0, 9 mod 8 is 1, etc.; 8 is considered equal (equivalent) to 0. In base 8, you don't say that 8 is equivalent to 10, since, as someone already pointed out, the symbol "8" has no meaning. What you say is that this many things: XXXXXXXX represented by the symbol "8" in base 10, is represented by the symbols "10". Pedantic mathematician mode=off. We now return you to our regularly scheduled WTF.
Admin
Or Silverlight.
Admin
Except, if you're from some strange part of the world that uses QWERTY, but doesn't have the US-layout. I have two buttons for +, and both requires just a single keystroke. (The one on the numpad requires moving my hand a lot though)
Admin
I get the numpad + that requires only one keystroke, where's the second? The only other one I see is above the equals sign, which requires a shift and the key - two keystrokes. Am I missing one?
Admin
The way javascript handles dates is pretty nice once you realize how easy this "WTF" makes date/time math.
Everything gets converted into milliseconds, and then the date object is milliseconds since the epoch (1/1/1970). This means that adding/subtracting any unit of time will get converted to milliseconds and the date that will be displayed will be converted back to the usual human readable formats.
Time becomes single number instead of having to handle each section of having to worry about all the different rules for how we keep track of time.
Admin
And where exactly is the WTF?
Ah, I see, some programmer was too lazy to read his "JavaScript for Dummies" until the end.