|
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: 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: 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:
letterCount += textWords[i].length; is equivalent to: letterCount = letterCount + textWords[i].length; and ++i; is equivalent to: i = i + 1; Which would you rather type? 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: 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: 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: 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: 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: 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.
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
|
If you like this project, you might enjoy exploring related careers.
![]() |
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. | |
![]() |
Software Quality Assurance Engineer & Tester Software quality assurance engineers and testers oversee the quality of a piece of software's development over its entire life cycle. Their goal is to see to it that the final product meets the customer's requirements and expectations in both performance and value. During the software life cycle, they verify (officially state) that it is possible for the software to accomplish certain tasks. They detect problems that exist in the process of developing the software, or in the product itself. They try and make things not work (try to "break" the software) by creating errors or combinations of errors that a user might make. For example, if a user enters a period or a pound sign for a password, will that break the software? They seek to anticipate potential issues with the software before they become visible. At the end of the life cycle, they reflect upon how problems or bugs arose, and figure out ways to make the software development process better in the future. | |||
|
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. |