Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active December 10, 2015 13:48
Show Gist options
  • Save niraj-shah/4442774 to your computer and use it in GitHub Desktop.
Save niraj-shah/4442774 to your computer and use it in GitHub Desktop.
Counting the words
var text = $('#text').text();
var word_array = text.split(' ');
// blank array to store result
var word_count = {};
// lets count the words
for ( var x in word_array ) {
// make the word lowercase
var word = word_array[x].toLowerCase();
// we are only interested in words with 3 or more characters
if ( word.length >= 3 ) {
if ( typeof word_count[word] != 'undefined' ) {
// if the word exists in the array, lets increase its count
word_count[word] += 2;
} else {
// if the word doesn't exist, lets set its initial count
word_count[word] = 2;
}
} // end if statement for word.length
} // end for look
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment