Patrick Stone wrote:
Here's a reason why javascript sucks so bad. The following *SIMPLE*
piece of code will not work properly. Jesus, how I yearn for the day
when a real programming language can be substituted for this patched
together scripting horror. I suspect the string object's methods are
simply not robust enough for this because neither of the strings
appear to have the value they should when this loop runs. Perl would
handle this kind of thing with no problem. Javascript definitely needs
to catch up. (Sorry about the rant, but for a long-time programmer
this is terribly frustrating!)
-----------------
for (x=0; x<=theStr.length; x++);
{
theNewStr += theStr.charAt(x);
}
-----------------
--pstone
In addition to everything everyone else has said, I'd like to point out
that JavaScript is hardly a "patched together scripting horror", and if it
*were*, this is certainly not evidence of it. The loop as written above
would do precisely the same thing in C, C++, C# and Java (assuming you
modified the code to conform to the particular nuances of the language you
choose), and none of those languages are "patched together scripting
horrors".
As for your suspicion that it has to do with some "lack of robustness" in
the String objects methods, try again. The String object has a
full-featured set of methods which do a fine job of even extremely complex
string manipulations.
By the way, your code, as written above, is equivilent to:
theNewStr = theStr;
If you are processing the string one character at a time in order to
identify and manipulate specific characters, there's probably a myriad of
better ways of doing it then by chugging through the string one character
at a time. Want to capitalize the first character of every word?
String.prototype.toMixedCase = function() {
function toUpper(sChar) { return sChar.toUpperCase(); }
return this.toLowerCase().replace(/\b(.)/g, toUpper);
}
theNewStr = theStr.toMixedCase();
Want to remove all the spaces from the string?
theNewStr = theStr.split(' ').join('');
// or
theNewStr = theStr.replace(/ /g, '');
Want to identify the '#' character and everything that comes after it, but
only if you find it in the string?
if (/#(.*)/.test(theStr)) {
alert('theStr contained a # and ' + RegExp.$1 + ' came after it');
}
So yeah, I guess you're right, JavaScript is just a non-functional mess
that can't do anything. Better go back to Perl where you can loop through
every character in a string to accomplish the same goals.
--
| Grant Wagner <gw*****@agricoreunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
*
http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
*
http://www.mozilla.org/docs/web-deve...upgrade_2.html