Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement