HighDots Forums  

Re: working word counter

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: working word counter in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Grant Wagner
 
Posts: n/a

Default Re: working word counter - 02-26-2004 , 02:47 PM






David wrote:

Quote:
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 <url: http://jibbering.com/faq/#FAQ4_16 />

Quote:
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();

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