PDA

View Full Version : word count


jasonc65
01-08-2003, 12:45 PM
I'm trying to implement a word counter that counts returns as spaces and does not count multiple spaces/returns more than once.

The code I have here does not quite work:

function countit() {

var i=0
var formcontent=document.wordcount.wordcount2.value
for(i=0; i<formcontent.length; i++) {
if(formcontent[i]=='\n'||formcontent[i]=='\r') formcontent[i]=' '
}
document.wordcount.wordcount2.value=formcontent
formcontent=formcontent.split(" ")
var formlength=formcontent.length
for(i=0; i<formcontent.length; i++) {
if(formcontent[i]=="") formlength--
}
document.wordcount.wordcount3.value=formlength
}

Unfortunately, I can't indent.

chrismiceli
01-08-2003, 01:41 PM
function countit() {

var i=0
var formcontent=document.wordcount.wordcount2.value
for(i=0; i<formcontent.length; i++)
if(formcontent[i]=='\n'||formcontent[i]=='\r') formcontent[i]=' ' // you never split formcontent, it is just a string from an input value.
document.wordcount.wordcount2.value=formcontent
formcontent=formcontent.split(" ")
var formlength=formcontent.length
for(i=0; i<formcontent.length; i++)
if(formcontent[i]=="") formlength--
document.wordcount.wordcount3.value=formlength
}

jasonc65
01-08-2003, 02:01 PM
I do split the string later. Pay attention. What I was doing first was trying to replace "\n" and "\r" with " " throughout the string.

Adam20002
01-08-2003, 03:43 PM
You could use a regular expression



formcontent = formcontent.replace(/\s+/g," ");



something like that should replace all multiple spaces \t\r etcs into one " ".

Adam

jasonc65
01-08-2003, 04:49 PM
Adam, could you 1) explain that syntax, since it is neither a string literal nor a numerical constant and 2) explain why my for loop didn't do the job, like it would have in C?

jasonc65
01-08-2003, 05:16 PM
Thanks for your help, both of you.

I now got a working version of my code, which goes like this:

function countit(){
var i=0
var formcontent=document.wordcount.wordcount2.value
formcontent = formcontent.replace(/\s+/g," ");
formcontent=formcontent.split(" ")
var formlength=formcontent.length
if(formcontent[0]=="") formlength--
if(formcontent[formcontent.length-1]=="") formlength--
if(formlength<0) formlength=0
document.wordcount.wordcount3.value=formlength
}

Notice that after splitting, formcontent is an array of strings. By comparing the fist and last string with the empty string, I can take care of the problem that arises when spaces or returns are at the beginning or end. But a string apparently is not simply an array of chars, whatever they are. This is one part of JS that is not like C/C++.

Adam20002
01-08-2003, 10:32 PM
Hi Jason,

Glad you got your code working, sorry i couldn't reply earlier.

Notice that after splitting, formcontent is an array of strings.

looking back i think this is what chrismiceli was getting at when he indicated that you needed to be splitting the string earlier in the code.

whammy
01-09-2003, 01:02 AM
Dang, I have a MUCH shorter script that does the same thing...

http://www.solidscripts.com/displayscript.asp?sid=3


<script type="text/javascript">
<!--
function countwords(x){
var w = x.split(/\s+/);
return(w.length);
}
// -->
</script>


P.S. That also accounts for leading and trailing spaces, etc. apparently. Did better than I thought! Lemme know if there are any problems with it. :)

Steven_Smith
01-09-2003, 01:06 AM
regular expressions to the rescue!

jasonc65
01-09-2003, 01:35 AM
Whammy, I tested your code and it has one slight problem. The null string returns 1, while a string of spaces only returns 0. Go figure.

whammy
01-09-2003, 01:36 AM
yeah, I just discovered that as well. Here's the fix!:

<script type="text/javascript">
<!--
function countwords(x){
var w = x.split(/\s+/);
return (w != "") ? w.length : 0;
}
// -->
</script>


P.S. Let me know if you find any other problems, I tested the **** out of that, though. :D

P.P.S. Edited to remove some apparently unnecessary code and the fact that javascript apparently sees a string length of 1 when no string really exists, after a few other tests! ;)