By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

word counter script locks up Mozilla

P: n/a
Hey folks,

I somehow managed to create a script that locks up Mozilla (1.3)
tight. Here's the culprit, perhaps y'all can provide some insight
into what the trouble is. (This script was cobbled together from
several different word-count routines I found on the web, none of
which worked correctly in all cases.)

function trim(data)
{
i=0;
while (/\s/.test(data.charAt(i)))
{data=data.substring(i,data.length);}
i=data.length;
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}
return data;
}

function countWords()
{
input=document.forms[0].elements[0].value;
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;
return output;
}

activated via a button: onClick="alert(countWords())"

Thanks for any help,
--David
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
David wrote:
Hey folks,

I somehow managed to create a script that locks up Mozilla (1.3)
tight. Here's the culprit, perhaps y'all can provide some insight
into what the trouble is. (This script was cobbled together from
several different word-count routines I found on the web, none of
which worked correctly in all cases.)

function trim(data)
{
i=0;
while (/\s/.test(data.charAt(i)))
{data=data.substring(i,data.length);}
i=data.length;
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}
return data;
}

function countWords()
{
input=document.forms[0].elements[0].value;
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;
return output;
}

activated via a button: onClick="alert(countWords())"


I tested it, as you have it, in Moz1.4 and it worked flawlessly, so it
must be something they changed in 1.3/1.4

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #2

P: n/a
da***********@mindspring.com (David) writes:
I somehow managed to create a script that locks up Mozilla (1.3)
tight. Here's the culprit, .... i=0;
while (/\s/.test(data.charAt(i)))
{data=data.substring(i,data.length);}
If the first character of data is a space, the the test tests true,
and then you execute
data = data.substring(0,data.length);
which is the same as
data = data
Infinite loop => no response from browser.

So, you don't remove the space. You loop never ends, as the next test
also finds a space at the beginning.

I guess it should be
data = data.substring(1,data.length);
instead.
i=data.length;
Then i is set to the length of data ....
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}
and used as an index into the string. As an index, the length
is one larger than the last character of the string, so
data.charAt(i)
gives the empty string, which doesn't match. You don't remove
any space from the end of the string.

So, your trim function is totally faulty: crashes on initial
whitespace, and ignores termination.

Try this one instead:

function trim(str) {
return str.replace(/^\s*/,"").replace(/\s*$/,"");
}

function countWords()
{
You should declare your local variables so you don't overwrite
global ones:
input=document.forms[0].elements[0].value; var input = ... input=trim(input);
wordList=input.split(/\s+/g); var wordList = ... output=wordList.length; var output = ... return output;
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
Randy Webb <hi************@aol.com> wrote in message news:<_J********************@comcast.com>...
I tested it, as you have it, in Moz1.4 and it worked flawlessly, so it
must be something they changed in 1.3/1.4


I forgot to mention, the key to the lockup is a space before the first
word in the textarea. Although after tweaking heavily I managed to
keep the error from appearing and got the word counter working with
everything I can think to throw at it, I'm still curious why this
previous version crashes the browser. I would have thought that the
javaScript engines would be "wise" enough to circumvent endless loops
or other typical programming bugs with an error message or something.
Naive to think so? Seems kinda risky for any old random
poorly-crafted web script to be able to lock everything up like that.

--David
Jul 20 '05 #4

P: n/a
JRS: In article <67**************************@posting.google.com >, seen
in news:comp.lang.javascript, David <da***********@mindspring.com>
posted at Sat, 14 Feb 2004 23:01:57 :-
(This script was cobbled together from
several different word-count routines I found on the web, none of
which worked correctly in all cases.)


That, of course, is the problem.

First, one must define "word" -
is door-mat one word or two?
is cat's one word or two?
how may words in cat's-paw?
is Q a word?
how many words in bb22cc?

The simple case is if a word is any string of characters from one set,
separated by any number of characters not in the set.

For test purposes, let the good set be the characters matched by RegExp
\w, so the bad set is matched by \W.

S = "the quick brown fox "
X = S.replace(/\w+/g, 'a').replace(/\W+/g, '').length

gives X=4; there is no need to pre-trim S.

If it is necessary to count words in many or long strings, do speed
tests of different methods.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<d6**********@hotpop.com>...

If the first character of data is a space, the the test tests true, .... So, you don't remove the space. You loop never ends, as the next test
also finds a space at the beginning.
Ah, so! My heavily tweaked script now seems to work great, even with
mulitple whitespace characters encountered before, between, and after
the words. I will post the complete, working script in another thread
("working word counter") for public reference.

.... You should declare your local variables so you don't overwrite
global ones:


Good advice. Done and done. Thanks for the help.
--David (still amazed that endless loops aren't trapped somehow by the
browser)
Jul 20 '05 #6

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<TU**************@merlyn.demon.co.uk>...
First, one must define "word" -
is door-mat one word or two?
is cat's one word or two?
how may words in cat's-paw?
is Q a word?
how many words in bb22cc?

<snip>

Too true; for my purposes I settled on "any group of characters
separated by whitespace" and used the regexp sequence /\s/ as the
definition of whitespace characters (which I think includes space,
carriage return, linefeed, tab, etc.). I will post the complete (now
working) script in a new thread "working word counter". It worked
instantaneously on an ~1800 word essay, which is what I originally
wanted the counter for.

Thanks for the help Dr. John!
--David
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.