O'Reilly logo

JavaScript Application Cookbook by Jerry Bradenbaugh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

strings.js

Practical uses

String manipulation, alphabetization, frequency counting

Version requirement

JavaScript 1.2

Functions

camelCaps(), prepStr(), wordCount(), reorder()

The functions give you a taste of what you can do with strings, most likely from user input. Open ch06\string.html in your browser and Figure 6.16 is what you’ll see.

Three forms to crunch string data, starting with word count

Figure 6-16. Three forms to crunch string data, starting with word count

What you have here are three forms that demonstrate three functions. The first form contains a TEXTAREA to enter text. After the user enters text and presses the “Count” button, function wordCount() generates a new page with a table. The table list each of the “words” typed in the TEXTAREA and the number of times the word was encountered. You can see the results in Figure 6.17.

The table of words and their respective counts

Figure 6-17. The table of words and their respective counts

The second form contains a TEXTAREA for entering text, too. The user can then choose “Upper” or “Lower” to convert the first character of each word to upper- or lowercase. Function camelCaps() takes care of this. You might find this function handy for form validation, perhaps when users have to enter names and addresses. Check out the case change in Figure 6.18.

Figure 6-18. Capitalizing the first character or each word

Figure 6-19. Alphabetizing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required