Last active
December 10, 2015 12:58
-
-
Save niraj-shah/4438264 to your computer and use it in GitHub Desktop.
Complete Word Cloud JS File
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
$(document).ready(function() { | |
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 | |
// output the result to the cloud div | |
for ( var y in word_count ) { | |
$('#cloud').append('<div class="tag" style="font-size: '+ word_count[y] +'px;">' + y + '</div>'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment