yaultB
11-16-2010, 05: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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Count</title>
<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++)
{
if(my_array[b]!="")
{
a++;
}
}
*/
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 = '';
}
</script>
<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">
<tr>
<td align="right">#of Sent</td>
<td><input name="cnStn" type="text" /></td>
</tr>
<tr>
<td align="right"># of Words in Sent</td>
<td><input name="cnWdsStn" type="text" /></td>
</tr>
<tr>
<td align="right"># of Letters Per Word</td>
<td><input name="cnLtrWds" type="text" /></td>
</tr>
<tr>
<td align="right">Avg Words Per Sentence</td>
<td><input name="avWdsStn" type="text" /></td>
</tr>
<tr>
<td align="right">Average Word Length</td>
<td><input name="avWdLen" type="text" /></td>
</tr>
<tr>
<td align="right">Word Array</td>
<td><input name="wrds" type="text" /></td>
</tr>
</tr>
<tr>
<td align="right">Sentence Array</td>
<td><input name="stces" type="text" /></td>
</tr>
</table>
</form>
</head>
<body>
</body>
</html>
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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Count</title>
<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++)
{
if(my_array[b]!="")
{
a++;
}
}
*/
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 = '';
}
</script>
<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">
<tr>
<td align="right">#of Sent</td>
<td><input name="cnStn" type="text" /></td>
</tr>
<tr>
<td align="right"># of Words in Sent</td>
<td><input name="cnWdsStn" type="text" /></td>
</tr>
<tr>
<td align="right"># of Letters Per Word</td>
<td><input name="cnLtrWds" type="text" /></td>
</tr>
<tr>
<td align="right">Avg Words Per Sentence</td>
<td><input name="avWdsStn" type="text" /></td>
</tr>
<tr>
<td align="right">Average Word Length</td>
<td><input name="avWdLen" type="text" /></td>
</tr>
<tr>
<td align="right">Word Array</td>
<td><input name="wrds" type="text" /></td>
</tr>
</tr>
<tr>
<td align="right">Sentence Array</td>
<td><input name="stces" type="text" /></td>
</tr>
</table>
</form>
</head>
<body>
</body>
</html>