Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. function getTokens(rawString) {
  2. // This function take a single argument 'rawString' which will have to be a string //
  3. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  4. // It returns the string with all lowercased words using `.toLowerCase()` //
  5. // Then with `.string(/[ ,!.";:-]+/)` it splits the string into an array while removing all special characters and spaces //
  6. // NB: `.filter(Boolean)` removes any falsy items from an array
  7. // Finally `.sort()` organizes the remaining words in alphabetical order //
  8. }
  9.  
  10. function mostFrequentWord(text) {
  11. // This function also takes a single argument 'text' which will also be a string //
  12. let words = getTokens(text);
  13. // The variable `words` is being defined as the function that was just described above with the argument changed to 'text' //
  14. let wordFrequencies = {};
  15. // The variable `wordFrequencies` is defined as an empty object for now //
  16.  
  17. for (let i = 0; i < words.length; i++) {
  18. // This `for` loop starts at the first index of the array when i=0 //
  19. // Then it runs until the index is less than or equal to the number of items in the `words` array (`i <= words.length`) //
  20. // It adds 1 to the index each time the loop is run with i++ //
  21. // There is a bug in the second argument in this loop - when `words[i]` is logged to the console, an extra item 'undefined' is logged //
  22. // To fix this, change the operator to '<' instead of '<=' //
  23. if (words[i] in wordFrequencies) {
  24. // This conditional takes every `words` index item and makes it a key in the object `wordFrequencies` //
  25. wordFrequencies[words[i]]++;
  26. // If the conditional is true, then it adds one count to the value of the key `words[i]` //
  27. } else {
  28. wordFrequencies[words[i]] = 1;
  29. // If it is false, the value of the key `words[i]` will be equal to 1 //
  30. }
  31. }
  32.  
  33. let currentMaxKey = Object.keys(wordFrequencies)[0];
  34. // The variable `currentMaxKey` is defined as the first key in the object `wordFrequencies` //
  35. let currentMaxCount = wordFrequencies[currentMaxKey];
  36. // The variable `currentMaxCount` is defined as the value of the `currentMaxKey` in the object `wordFrequencies` //
  37.  
  38. for (let word in wordFrequencies) {
  39. // This loop creates a variable `word` for every key within the object `wordFrequencies` and it runs until there are no more keys in the object //
  40. if (wordFrequencies[word] > currentMaxCount) {
  41. // This conditional compares the value of each key in `wordFrequencies` to the variable `currentMaxCount` //
  42. currentMaxKey = word;
  43. currentMaxCount = wordFrequencies[word];
  44. // If the value of `wordFrequencies[word]` is greater than the value of `currentMaxCount` then it returns the key of `word` as `currentMaxKey` and it sets `currentMaxCount` equal to the value of that key //
  45. }
  46. }
  47. // This function will return the value of currentMaxKey when it is called which should be the word that appears most frequently //
  48. return currentMaxKey;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement