Specific word counter for Javascript.

At my job where I do Internet tech support, I use Notepad to keep a log of the day’s calls. The entries that fill this log were generated by a database system that lets me pull up the customer’s account so I can document what happened during the call. These entries always start with the word “Agent” so by counting the number of times that word appears in my call notes, I can tally up how many calls I get.

One way to do the word count was loading Microsoft Word, pasting the call notes and then executing the Find command. Word would then tell me how many instances of the word “Agent” it found, but the process of loading such a large program like Word and navigating my way to the Find command seems a bit time-consuming. Surely there must be an easier, faster way to do this.

Then I found a web site with a handy word frequency counter that lists the words and the number of times they appear in the pasted text. No offense to WriteWords but it doesn’t feel right transmitting call notes full of customer phone numbers across the Internet to an unsecured web site just to find out how many calls I got.

I gave some thought to creating a simple HTML document using Javascript to count the number of occurences of the word “Agent” but didn’t know how to go about it since I am a total novice at Javascript. A search on the almighty Google found tons of scripts that count the total number of words in a block of text but I had a harder time finding a script to just count the number of instances of a single word.

Then I found this script which has the interface I’m looking for. I want to be able to paste my call notes into a text field and click a Submit button and have the script return the grand total and thought this was a good script to use.

I studied the script closely and took notice of the line that says:

words = str.split(” “);

I may not be familiar with Javascript but I am familiar with how programming languages work with strings, and no doubt this is a command to split the string into separate substrings wherever there’s a space between them. I got an idea and added the word “Agent”, thus:

words = str.split(“Agent”);

And it works. I paste my call notes in the input box, click the button and the script works its magic to give me the number of calls I got that day. I then made some cosmetic changes to the script to customize the text accordingly.

Here’s my modified script:

<HTML>
<HEAD>
<TITLE>Call Counter</TITLE>
<HEAD>
<SCRIPT>
function countWords(str){
var count = 0;
words = str.split(“Agent”);
for (i=0 ; i < words.length ; i++){
// inner loop — do the count
if (words[i] != “”)
count += 1;
}

document.theForm.results.value =
“You got ” +
count +
” calls.”;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=”theForm”>
Paste your call notes here.<p>
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<p>
<INPUT type=button value=”Count Calls”

onClick=”countWords(document.theForm.inStr.value)”;>
<p>
<TEXTAREA name=results rows=1 cols=20>
</TEXTAREA>
</FORM>
</BODY>
</HTML>

Another dilemma solved by JavaScript.

Thanks to Java2S for posting the tutorial.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s