Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. function wordCount(text) {
  2. return text.split(" ").length;
  3. };
  4.  
  5. function uniqueWordCount(text) {
  6. var words = text.split(" ");
  7. var counts = [];
  8. for(var i = 0; i < words.length; i++ ) {
  9. // if the word's position in the array exists, then don't push it into the empty array
  10. if(counts.indexOf(words[i]) === -1) {
  11. counts.push(words[i]);
  12. }
  13. }
  14. return counts.length;
  15. };
  16.  
  17. function averageWordLength(text) {
  18. var words = text.split(" ");
  19. var totalCharacters = 0;
  20. for(var i = 0; i < words.length; i++) {
  21. totalCharacters += words[i].length;
  22. }
  23. return (totalCharacters / words.length).toFixed(2);
  24. }
  25.  
  26. $(function() {
  27. $('button').click(function(abc) {
  28. abc.preventDefault();
  29. console.log(abc);
  30. // Get the words into an array
  31. // call all of our functions by passing in the data from the text area
  32. var text = $('textarea').val();
  33. $('#js-count').html(wordCount(text));
  34. $('#js-unique').html(uniqueWordCount(text));
  35. $('#js-average').html(averageWordLength(text));
  36. $('dl').removeClass('hidden');
  37. });
  38. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement