Guest User

Untitled

a guest
Jan 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function mostFrequentWord takes a single argument - text. variable words uses that argument to call getTokens function.
  2.  
  3. getTokens function
  4. getTokens function 'cleans' text.. first method changes all the letters to lower case, then `split` separates
  5. string into words if there is a space or any of the '/[,!.";:-]+/' symbols. Next, `.filter(Boolean)` removes any items
  6. that are not literals with true and false value. lastly `sort` arranges words in alphabetical order.
  7. getTokens function returns us an array of alphabetically organised separate words that we will use in mostFrequentWord.
  8.  
  9. second local variable in mostFrequentWord is empty object.
  10. for loop starts at 0 index and counts through words array taking one word at the time as an argument.
  11. if statement checks if that word has been already added into wordFrequencies object.
  12. If No then it places a key into an object assigns a value of 1. If it already exists, then it adds 1 to the value of the word.
  13.  
  14. When the loop is finished we have wordFrequencies object with `word` as key and number as a value of how many times
  15. that word occurred in the words array.
  16.  
  17. `currentMaxKey` is the first Key of wordFrequencies
  18. `currentMaxCount` is the value of `currentMaxKey`
  19.  
  20. for in loop iterates over the keys of the object `wordFrequencies` where `word` is the key. wordFrequencies[word] is the value of the current key.
  21. if the value of the current key is higher than the `currentMaxCount`, we change the value of the `currentMaxKey` to the current key (`word`)
  22. and we change the value of `currentMaxCount` to the value of the current key (`wordFrequencies[word]`)
  23.  
  24. if the value of the current key is NOT higher than the `currentMaxCount`, no reassignment occurs and the for in loop continues.
  25.  
  26. finally once the loop has finished we return the currentMaxKey.
Add Comment
Please, Sign In to add comment