Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. //function that takes an argument rawString and returns a sorted array of strings
  2. function getTokens(rawString) {
  3. // NB: `.filter(Boolean)` removes any falsy items from an array
  4.  
  5. //.toLowerCase() returns the lower case value of rawString
  6. //.split(/[ ,!.";:-]+/) splits the string into an array of substrings and a regular expression is used as the separator to separate the words of the string at any of the characters in the regex
  7. //now rawString is an array and .filter() method is used to create a new array with elements that pass the function Boolean which removes all falsy elements
  8. //then finally the .sort() method is used on the new array and sorts the array of strings by alphabetical order
  9. //a sorted array of strings with no falsy elements is returned by this function
  10. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  11. }
  12.  
  13. //function that takes an argument text and returns the updated most frequent word
  14. function mostFrequentWord(text) {
  15. //declares a variable called words and sets its value to the returned value of the function getTokens
  16. let words = getTokens(text);
  17.  
  18. //declares a variable called wordFrequencies and sets its value to an empty object
  19. let wordFrequencies = {};
  20.  
  21. //a for loop is used to iterate through the array returned by getTokens(text)
  22. for (let i = 0; i <= words.length; i++) {
  23.  
  24. //starting at index 0, if the element matches some key in wordFrequencies, then it will increment by one to the value of the key
  25. if (words[i] in wordFrequencies) {
  26. wordFrequencies[words[i]]++;
  27. }
  28. //else if the element does not match some key in wordFrequencies, then the element at words[i] will be added as a new key into wordFrequencies and its value will be set to 1
  29. else {
  30. wordFrequencies[words[i]] = 1;
  31. }
  32. }
  33.  
  34. //declares a variable called currentMaxKey and sets its value to the key at index [0]
  35. let currentMaxKey = Object.keys(wordFrequencies)[0];
  36.  
  37. //declares a variable called currentMaxCount and sets its value to the value of the key wordFrequencies[currentMaxKey]
  38. let currentMaxCount = wordFrequencies[currentMaxKey];
  39.  
  40. //a for in loop that iterates through the keys in wordFrequencies
  41. for (let word in wordFrequencies) {
  42.  
  43. //starting at the first key, if the value at wordFrequencies[word] is greater than the value at currentMaxCount, then currentMaxKey is set to word and the value at currentMaxCount is updated to the value at wordFrequencies[word]
  44. if (wordFrequencies[word] > currentMaxCount) {
  45. currentMaxKey = word;
  46. currentMaxCount = wordFrequencies[word];
  47. }
  48. }
  49.  
  50. //the function should return the key with the highest value/count
  51. return currentMaxKey;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement