Last active
December 10, 2015 13:48
-
-
Save niraj-shah/4442774 to your computer and use it in GitHub Desktop.
Counting the words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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