Created
February 22, 2017 06:12
-
-
Save MatthewJRoybal/89c7229e80589fd8a1e5c78225c6c12b to your computer and use it in GitHub Desktop.
A text analyzer script
This file contains 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
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