Yet another inexplicable js error!

P

Patrick Stone

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);
}
 
L

Lee

Patrick Stone said:
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);
}

It's interesting that the more a person rants about the lousy
programming language, the more obvious the coding errors are.
There are two pretty obvious errors in those few lines. We
can only guess what you've done wrong in your other code.

for (x=0; x<theStr.length; x++)
{
theNewStr += theStr.charAt(x);
}

I don't want to know why you would ever copy a string one
character at a time.
 
K

KC Wong

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);
}

Notice that there's a semi-colon after the for statement? I hope it's a
typing error when you're posting...


KC
 
K

kaeli

Here's a reason why javascript sucks so bad.

Because you aren't coding properly?

You put a semi-colon at the end. That ends the statement. It does
nothing but loop a number of times.
The language is fine once you can use it right.
I use JS, Perl, C, Java...they all work just fine for what I need them
for.
{
theNewStr += theStr.charAt(x);
}

Try
theNewStr = theStr;

There is no reason to copy character by character. Strings assign just
fine is JS.
If the goal is to eventually do a substring of a string, there's a
function for that. If the goal is to replace a middle piece, there's a
function for that, too. I can't think of any reason to do a character by
character copy...

http://www.w3schools.com/js/js_string.asp

-------------------------------------------------
~kaeli~
All I ask for is the chance to prove that money
cannot make me happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
G

Grant Wagner

Patrick said:
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);
}

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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
Z

Zac Hester

Grant said:
Patrick Stone wrote:

Here's a reason why javascript sucks so bad. [snippy-snip]

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.

The irony here is that Perl is built around complex string manipulation
and has a lot more string methods than JavaScript. Why he would ever
try to evaluate a string one character at a time in Perl is something
far beyond the grasp of any mortal programmer. Your arguments
supporting JavaScript apply to Perl in every way. In Perl, he should be
using:

$TheNewString = $TheString;

I've even found it more combersome to switch back to C-style string
manipulation (one character at a time) in Perl than to just RTM and find
out how to use one of the hundreds of string manipulation features.

I know how it feels to do everything the hard way for a while until I
crack open a book and say, "Oh, so that's how you do it..." I would
suggest to the OP a trip to the nearest bookstore that sells any
O'Reilly and Associates books. (Maybe "Programming Perl" would also be
a good buy alongside the venerable "JavaScript: The Definitive Guide.")

Happy reading,
Zac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top