View Full Version : Need Help with Strings and Substrings

11-16-2010, 06:01 PM
I'm new at JavaScript and am trying to figure out a simple text calculator as seen on a science project website.

The project says the final program should calculate:
1. the number of sentences contained in the text,
2. the number of words in each sentence,
3. the number of letters in each word,
4. the average number of words per sentence, and
5. the average word length.

I have pretty much everything (I think) but am being completely stumped by item number 2 and item number 4. The code is below.

Can anyone help me understand what I should be doing for point number 2 and 4? I understand how to calculate and display the length of an item in an array and return it's values as 5,6,7,8 etc where the value is the length of the word, but i can't grasp how to calculate the number of items in an array to read 2,5,9 where the values are the number of words per each sentence...so confused!

Below is my code...apoligies if it's sloppy...it's my first javascript code experience

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">

function callText(mainText, stnCount, wdsPerStn, letPerWd, avgWdPerStn, avgWdLen, testFieldWds, testFieldStnc){
var textWords = mainText.value.split(/\s/);//splits into words by each space.
var textSent = mainText.value.split('.');//splits into sentences by each period.

var nWords = new Array(textWords.length);
var totalLetters = 0;
for (i = 0; i < textWords.length; i++)
nWords[i] = textWords[i].length;
totalLetters += textWords[i].length;

var nSent = new Array(textSent.length);
var totalSent = 0;
for (i = 0; i < textSent.length; i++)
nSent[i] = textSent[i].length;
totalSent += textSent[i].length;
/* var b,a=0;
var my_array = new Array(nSent);
for(b=0;b<my_array.length ;b++)
var newStr = nWords.substr(0).length;
stnCount.value = textSent.length - 1;
wdsPerStn.value = 'wdsPerStn';
letPerWd.value = nWords.join(', ')
avgWdPerStn.value = 'avgWdPerStn'
avgWdLen.value = totalLetters / textWords.length;
testFieldWds.value = 'testFieldWds';
testFieldStnc.value = 'testFieldStnc'


function clearForm(form){
form.inputArea.value = '';
form.cnStn.value = '';
form.cnWdsStn.value = '';
form.cnLtrWds.value = '';
form.avWdsStn.value = '';
form.avWdLen.value = '';
form.wrds.value = '';
form.stces.value = '';

<form action="" method="post" name="textForm">
<p><textarea name="inputArea" cols="80" rows="5"></textarea></p>
<p><input name="calculate" type="button" value="Calculate" onclick="callText(inputArea, cnStn, cnWdsStn, cnLtrWds, avWdsStn, avWdLen, wrds, stces )" /> <input name="clearform" type="button" value="Clear" onclick="clearForm(form)" /></p>
<table width="325" border="0">
<td align="right">#of Sent</td>
<td><input name="cnStn" type="text" /></td>
<td align="right"># of Words in Sent</td>
<td><input name="cnWdsStn" type="text" /></td>
<td align="right"># of Letters Per Word</td>
<td><input name="cnLtrWds" type="text" /></td>
<td align="right">Avg Words Per Sentence</td>
<td><input name="avWdsStn" type="text" /></td>
<td align="right">Average Word Length</td>
<td><input name="avWdLen" type="text" /></td>
<td align="right">Word Array</td>
<td><input name="wrds" type="text" /></td>
<td align="right">Sentence Array</td>
<td><input name="stces" type="text" /></td>





Philip M
11-16-2010, 07:14 PM
Counting words and sentences is not as as easy as that - you must allow for punctuation marks and multiple spaces.

This may help you but I do not see the project as being suitable for a beginner. You ought not to try to run before you can walk! :)

<script type = "text/javascript">

var sentences = [];
var sentsplit = [];
var totalWords = 0;
var txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum. Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc. Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna mauris, pellentesque at ultricies vitae, fermentum vitae dolor. "
txt = txt.replace(/\s{2,}/g," "); // Replace multiple spaces with one space
txt = txt.replace(/\.|\?|\!/g,"~"); // Replace . ? ! by tilde
txt = txt.replace(/\,|\;|\:/g,""); // Strip comma, colon,semi-colon
txt = txt.replace(/(\s)(A-Z)/g,"$2"); // Strip space before capital letter
txt = " " + txt; // add space at start of text

sentences = txt.split("~");
var len = sentences.length-1;
alert ("There are " + len + " sentences");

for (var i = 0; i < len; i++) {
var sent = sentences[i];
sentsplit[i] = sent.split(" ");
words = sentsplit[i].length-1;
totalWords += words;
alert ("The total number of words is " + totalWords)

var averageWords = (totalWords/len).toFixed(2);
alert ("The average number of words in a sentence is " + averageWords);

BTW, when posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

So - did anyone dare tell George Stephenson, "It's not Rocket science"?

11-16-2010, 08:19 PM
Thanks Philip. Yes, I have taken on a bit more than I expected.
However, I was able to trudge through and get most of what I needed with the intent to go back after the initial calculations were set up and then add the required code to replace punctuation as you show above. Thanks for that code by the way. It helps me understand where to put specific code.

However, my main problem up front before I move on to replacing punctuation, double spaces and the like, is returning the number of words in each sentence and then the average number of words per sentence. Basically, to return the fields "# of Words in Sent" and "Avg Words Per Sentence" in the same fashion as the "# of letters Per Word" where the values are relative to word count, not letter count.

Like the pic below which is the form I created to handle the script:


That's the stopping point right now which has me in gridlock.

also...apologies on not wrapping my code properly. I think I edited and updated correctly.

Philip M
11-16-2010, 08:54 PM
A sentence terminates with a full stop (period), ! or ?. You need to count the number of sentences, the number of words, and the number of individual letters. A space or a punctuation mark is not a word, nor do they count towards the number of letters.

If you can establish these correctly then obviously the average number of words per sentence, the average length of a word etc. is simple maths.

Old Pedant
11-16-2010, 09:12 PM
Hey, Philip, I follow all of that except this one:

txt = txt.replace(/(\s)(A-Z)/g,"$2"); // Strip space before capital letter


The summers are hot in Africa.


The summers are hot inAfrica~

and the word count is now off.

Philip M
11-16-2010, 09:37 PM
Hey, Philip, I follow all of that except this one:

txt = txt.replace(/(\s)(A-Z)/g,"$2"); // Strip space before capital letter


and the word count is now off.

Oh dear! I don't know how that got in - copy and paste went wrong. :o
Just delete that line.