Day 24 – Word Count

I wanted to look at keyboard input today. I created a textarea where users could enter text. Every time there was a .keyup() event, the value in the form element would be retrieved with .val() and stored as a variable.

Using .length() would get me the number of elements (in this case, characters) of the stored string. To get the number of words, I split the string by empty spaces and then used .length() to get the number of elements. This is a simple way to do a word count of a form. However, this does not take into account punctuation. Punctuation such as an exclamation mark would be counted as one word. Entering “hello !” would give you two words.

Spaces can also cause problems. In this case, entering a string of spaces after the text results in much bigger number counts.
Space errorThere is definitely a lot to improve on! I think it would be fun to write a “write-or-die” type of page. Write or Die definitely saved me during Nanowrimo in 2013. I wonder if a “write-or-die” for code would result in more productivity or just many many bugs…

Day 24 – Word Count


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s