working word counter

D

David

After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

<html>
<!--Generated 02/12/04-->
<!--modified 02/15/04-->
<!--name of this file: "wordCounter.html" -->

<head>
<title>Word Counter</title>
<SCRIPT LANGUAGE="JavaScript">

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

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

</SCRIPT>
</head>

<body bgcolor="#666666"
text="#00ff00"
link="#ffffcc"
vlink="#ffffcc">

Enter or paste text into the box below and press <b>count</b> to see
the total number of words.
<br>
<br>
<form onSubmit="return false">
<center>
<textarea rows="30" cols="100"></textarea>
<br>
<br>
<button onClick="alert('total words = ' + countWords())"
class="button">count</button>
</center>
</form>

</body>
</html>
 
G

Grant Wagner

David said:
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

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

trim() would be better implemented as:

String.prototype.LTrim = new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim = new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim = new Function("return
this.replace(/^\\s+|\\s+$/g,'')")

as shown in the FAQ at said:
function countWords()
{
var input, wordList, output;
input=document.forms[0].elements[0].value;
input=trim(input);

Using the Trim() method outlined above, this would become:

input = input.Trim();

--
| 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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script.
<!--Generated 02/12/04-->

this is an international newsgroup; please use internationally-
comprehensible notation. The above, in most countries, is 2nd December
2004. The correct form is 2004-02-12; or put the month as Feb and use
YYYY.
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;

What purpose does trim serve here? and /g?

I find
Q = X.replace(/\S+/g, 'a').replace(/\s+/g, '').length
to be significantly faster than
Q = X.split(/\s+/).length
for strings with many words. The latter creates many Objects.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top