HighDots Forums  

Yet another inexplicable js error!

Javascript JavaScript language (comp.lang.javascript)


Discuss Yet another inexplicable js error! in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Patrick Stone
 
Posts: n/a

Default Yet another inexplicable js error! - 09-24-2003 , 12:48 AM






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

Reply With Quote
  #2  
Old   
Lee
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 01:12 AM






Patrick Stone said:
Quote:
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.



Reply With Quote
  #3  
Old   
KC Wong
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 01:15 AM



Quote:
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




Reply With Quote
  #4  
Old   
Spamless
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 01:53 AM



In article <f24660dd.0309232148.1b437bc7 (AT) posting (DOT) google.com>, Patrick Stone wrote:

Quote:
Perl would handle this kind of thing with no problem.
No it wouldn't.

Perl would require the for loop to have a bracket expression
before terminating:

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

instead of your

for (x=0; x<=theStr.length; x++);

(if you want to terminate the for loop before
doing anything, as you are doing)

Quote:
-----------------

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

-----------------

--pstone

Reply With Quote
  #5  
Old   
kaeli
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 07:42 AM



In article <f24660dd.0309232148.1b437bc7 (AT) posting (DOT) google.com>,
pstone67dog (AT) hotmail (DOT) com enlightened us with...
Quote:
Here's a reason why javascript sucks so bad.
Because you aren't coding properly?

Quote:
-----------------

for (x=0; x<=theStr.length; x++);
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.

Quote:
{
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
-------------------------------------------------


Reply With Quote
  #6  
Old   
Grant Wagner
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 02:46 PM



Patrick Stone wrote:

Quote:
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.

--
Quote:
Grant Wagner <gwagner (AT) agricoreunited (DOT) 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




Reply With Quote
  #7  
Old   
Zac Hester
 
Posts: n/a

Default Re: Yet another inexplicable js error! - 09-24-2003 , 10:02 PM



Grant Wagner wrote:

Quote:
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



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.