Skip to content

Instantly share code, notes, and snippets.

@MatthewJRoybal
Created February 22, 2017 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewJRoybal/89c7229e80589fd8a1e5c78225c6c12b to your computer and use it in GitHub Desktop.
Save MatthewJRoybal/89c7229e80589fd8a1e5c78225c6c12b to your computer and use it in GitHub Desktop.
A text analyzer script
function wordCount(text) {
return text.split(" ").length;
};
function uniqueWordCount(text) {
var words = text.split(" ");
var counts = [];
for(var i = 0; i < words.length; i++ ) {
// if the word's position in the array exists, then don't push it into the empty array
if(counts.indexOf(words[i]) === -1) {
counts.push(words[i]);
}
}
return counts.length;
};
function averageWordLength(text) {
var words = text.split(" ");
var totalCharacters = 0;
for(var i = 0; i < words.length; i++) {
totalCharacters += words[i].length;
}
return (totalCharacters / words.length).toFixed(2);
}
$(function() {
$('button').click(function(abc) {
abc.preventDefault();
console.log(abc);
// Get the words into an array
// call all of our functions by passing in the data from the text area
var text = $('textarea').val();
$('#js-count').html(wordCount(text));
$('#js-unique').html(uniqueWordCount(text));
$('#js-average').html(averageWordLength(text));
$('dl').removeClass('hidden');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment