Abstract
This is a more challenging first-time programming project. You'll learn how to use JavaScript to create a simple program to analyze one or more paragraphs of text. Your program will count sentences, words and letters, and report the resulting statistics. You'll be able to run your program in your Web browser.Objective
The objective of this project is to write a JavaScript program to make some basic measurements on a block of text:
Introduction
This is an example of a more advanced first-time programming project. You will be writing a simple program to gather statistics on a block of text. The project uses JavaScript, an interpreted programming language supported by most Web browsers.
PrerequisitesIt is assumed that you know how to use a text editor to write an HTML file containing your program, and that you know how to load and run the file in your browser.
You will also need to know the basics of how JavaScript functions can be written to work with HTML FORM elements. Specifically, you will be working with HTML TEXTAREA and INPUT elements, and using JavaScript String and Array objects in your functions.
If you need help with any of this, read the Introduction section in: "ABC's of Programming: Writing a Simple "Alphabetizer" with JavaScript."
New MaterialIn this project, you will learn some important methods of program control logic, which you will use again and again as you write more programs. These methods are used in just about every programming language (though the actual syntax may vary slightly from language to language). Specifically, you will learn about the JavaScript "for" and "while" loop control statements, and "if...else"conditional statements. You will also learn about 2-dimensional arrays (lists of lists).
Writing a JavaScript Program to "Measure" a Block of TextThe goal is to write a program that can take a block of text and calculate:
To tackle this, as with any programming project, a good way to start is to break it down into manageable sub-tasks. So for starters, let's try counting the number of letters in each word.
Step One: Loops for Counting Letters in Each WordWe'll assume that we've retrieved the text from the form, split it into the separate words, and stored the words in an array. To make it specific, let's say that the input text was: "the quick brown fox jumped over the lazy dog", and our array is called "textWords". Our array, then, looks like this:
textWords[0]=the
textWords[1]=quick
textWords[2]=brown
textWords[3]=fox
textWords[4]=jumped
textWords[5]=over
textWords[6]=the
textWords[7]=lazy
textWords[8]=dog
To count the number of letters per word, we need to step through the array, and get the length of each element. To do this, we use a common programming control technique: a loop.
JavaScript (and many other languages) has two ways of writing a loop, the "for" statement and the "while" statement. The following code snippet uses a while loop to count the number of letters in our text input:
var nWords = textWords.length;
var nLetters = new Array(nWords); // array for word lengths
var totalLetterCount = 0;
var nLettersPerWord = 0;
var i = 0; // loop counter
while(i < nWords)
{
nLetters[i] = textWords[i].length;
totalLetterCount += textWords[i].length; // running total of word lengths
++i; // increment the loop counter
}
nLettersPerWord = totalLetterCount/nWords;
The number of words, nWords, is equal to the length of our array (remember that the Array object's .length property is equal to the number of elements in the array). We create an array, nLetters, to store the length of each word. nLetters has the same number of elements as our textWords array. We also create variables to keep track of the running total of the letter count, totalLetterCount, and a counter variable, i, to keep track of where we are in the array. Notice how we named the variables to reflect their function. We also initialized (assigned initial values) to all of the variables, which is a good habit to get into.
The while loop works like this: as long as the condition in parentheses (i < nWords) evaluates to true, the program will continue to execute the statements contained in the following set of braces. So as long as the counter has not reached the end of the array, the program will continue to loop. The three statements within the braces instruct the program to:
When the program has iterated through the loop 9 times, the loop counter, i, will be equal to nWords. The while condition will now evaluate to false and the program will proceed to the first statement following the closing brace.
The for loop construction (below) is similar, but it keeps track of all the counter bookkeeping in one place. Here is the same code snippet, using a for loop:
var nWords = textWords.length;
var letterCount = 0;
var nLettersPerWord = 0;
for(var i = 0; i < nWords; ++i)
{
letterCount += textWords[i].length;
}
nLettersPerWord = letterCount/nWords;
The for construction has the statement for initializing the counter (var i = 0;), the condition statement to evaluate to test if the statements between the braces should be executed (i < nWords;), and the statement to increment the counter (++i). (Note that there is no semicolon after increment statement, just as there is no semicolon following the condition in the while statement.)
How do you choose which loop method to use? If your program has enough information to calculate how many times it needs to iterate (go through) the loop, then a for statement is usually clearest. It's easier to keep track of the loop counter if all of the statements are together in one place. So, for iterating through an array, the for loop is usually the best choice. In other cases, say when you are repeatedly processing input from the user and waiting for a special input to signal that it is time to go on to something else, then a while loop makes more sense.
With what you know now, you should be able to come up with something like this:
Simple Word Length Counter
It's a start, and works well enough for word lists separated by spaces. But what happens if you paste in ordinary text, punctuation and all? Aha! Punctuation marks get counted as letters. Read on to learn how to eliminate those pesky commas, periods, question marks and the like from your word lists.
Step Two: Nested Loops and Conditional StatementsSuppose that instead of counting all the letters in the textWords array, you only wanted to count how many times the letter "e" was used. We can use a conditional statement to modify our previous letter-counting loop so that only e's are counted. Here's how:
var eCount = 0;
for(var i = 0; i < textWords.length; ++i){ // outer loop
for(var j = 0; j < textWords[i].length; ++j){ // inner loop
if(textWords[i].toString().charAt[j] == 'e' ||
textWords[i].toString().charAt[j] == 'E'){ // if
++eCount;
} // end of if
} // end of inner loop
} // end of outer loop
Now we have two nested loops , the first one, with counter i, to step through the array of words, and the second one, with counter j, to step through the individual characters within each word. (Notice how we've used increasing indentation to identify the loops—this is where a text editor specifically for programming comes in handy.) We've used the Array object's .toString() method, and then the String object's .charAt() method to step through the individual characters. The statement:
if(textWords[i].toString().charAt(j) == 'e' || textWords[i].toString().charAt(j) == 'E')
means "if this character is a lowercase 'e' OR if this character is an uppercase 'E', then execute the statements between the following braces (otherwise, skip them)". The == is a comparison operator, (like < or >). If the two operands are equal, the comparison evaluates to true, if the two operands are not equal, the comparison evaluates to false. (Note that the equality comparison operator, ==, is not the same as the assignment operator, =, which assigns the value of the right-hand operand to the left-hand operand.) The || is a logical operator, in this case logical OR.
The previous example showed how for loops can be nested. Conditional loops can be nested as well. Suppose that you wanted to watch your P's and Q's in the sample text. You would first check to see if the character was a "P", then check to see if it was a "Q", then go on to the next letter. Here's how:
var pCount = 0;
var qCount = 0;
for(var i = 0; i < textWords.length; ++i){
for(var j = 0; j < textWords[i].length; ++j){
if(textWords[i].toString().charAt(j) == 'p' ||
textWords[i].toString().charAt(j) == 'P'){
++pCount;
}
else{
if(textWords[i].toString().charAt(j) == 'q' ||
textWords[i].toString().charAt(j) == 'Q'){
++qCount;
}
}
}
}
Here we've used the else construction to tell the program what to do when the first if statement evaluates to false (i.e., when the character is not a lowercase or uppercase 'p'). In that case, there is a nested if statement, which checks to see if the character is a 'q'. If it is, qCount is incremented, otherwise, the loop continues.
The project "ABC's of Programming: Writing a Simple "Alphabetizer" with JavaScript" shows how to use the JavaScript String object's .split() method to break a block of text into separate "words". However the .split() method is quite limited. For example, there can be only a single pattern for word breaks, but words in ordinary text are separated by both spaces and punctuation marks. You should be able to apply what you've learned about loops and conditional statements to write your own splitWords() function. It should take a paragraph of ordinary text as input and create an array of words as output. You can use nested for loops to iterate through each character in the text. Within the inner for loop you can use nested if...else statements evaluate each character and decide what to do with it.
Step Three: Using Two-Dimensional ArraysYou've already learned that an array is a list, for example, a list of all of the words in a sentence. A two-dimensional array is a list of lists. Following the previous example, our two-dimensional array would be a list of sentences, with each of the sentences consisting of a list of words.
Here's another example. Instead of sentences, just think of lines of text. Each line is an array of words, and each block of text is an array of lines. If our lines of text were:
the quick brown fox
jumped over the lazy dog
then a two-dimensional array containing all the words, line-by-line, would look like this:
lines[0][0]=the
lines[0][1]=quick
lines[0][2]=brown
lines[0][3]=fox
lines[1][0]=jumped
lines[1][1]=over
lines[1][2]=the
lines[1][3]=lazy
lines[1][4]=dog
The first index number indicates the line number, and the second index number indicates the position of the word within that line. You can use nested loops to iterate through all of the lines (outer loop), and all of the words within each line (inner loop).
As you might imagine, it keeps on going: arrays can have three (or even more) dimensions. Continuing our example, the next dimension would be lists of lines (paragraphs, maybe, or pages), and the next dimension would be lists of these (chapters or books), and so on.
You can use two-dimensional arrays in your project to make lists of sentences, which, in turn, are made up of lists of words.
We've covered a lot of ground! If you've gotten this far, you should be ready to write a program to "measure" some text.
Note for JavaScript files with Internet Explorer: If you want to continue to use Internet Explorer, try adding the following line at the beginning of your file: |
Terms, Concepts and Questions to Start Background Research
To write a simple program in JavaScript, you should do research that enables you to understand the following terms and concepts:
Bibliography
Materials and Equipment
Experimental Procedure
Here are some generally applicable programming tips to keep in mind as you get started with this project.
Variations
Credits
Andrew Olson, Ph.D., Science Buddies
Last edit date: 2006-04-17 19:42:17
If you like this project, you might enjoy exploring careers in Computer Science.
![]() |
Computer Programmer Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. |
![]() |
Computer Software Engineer Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. | |
![]() |
Network Systems and Data Communications Analyst Computers are an important part of our lives. We use computers to hold and process data, to control manufacturing factories, and to surf the Internet. We are all part of many different kinds of computer networks that are continually sharing information. The role of the network systems and data communications analyst is to design, model, and evaluate computer networks so that they can share information seamlessly. This is an exciting career for those people who enjoy working with rapidly changing technology. |
![]() |
Computer Hardware Engineer Whether you are playing video games, surfing the Internet, or writing a term paper, computers are an integral part of our daily lives. Computer hardware engineers work to make computers faster, more robust, and more cost-effective. They design the microprocessor chips that make your computer function, along with the equipment that makes computing easy and fun to do. | |
|
Join Science Buddies
Become a Science Buddies member! It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives. |