Skip to content

Instantly share code, notes, and snippets.

@dexter-stpierre
Created September 17, 2019 16:34
Show Gist options
  • Save dexter-stpierre/8e63d144896b247b7494ae698c4543a7 to your computer and use it in GitHub Desktop.
Save dexter-stpierre/8e63d144896b247b7494ae698c4543a7 to your computer and use it in GitHub Desktop.
# Answers
1. Create function called `getTokens`. This function takes an argument called `rawString` which is a string and returns array of all words in the string in alphabetical order
2. Comment
3. Sets the string to lowercase, splits it on characters that normally seperate words, filters out any falsy values, sorts the words alphabetically , and then returns that result
4. End of function
5. Whitespace
6. Create function called `mostFrequentWord`. This function takes an argument called `text` which is a string and returns a string of the most frequently used word in the string
7. Call the getTokens function and set the return value to a variable called `words`
8. set a variable called `wordFrequencies` fo an empty object. This object will hold key-value pairs indicating how often a word occurs in `text`
9. Loop through the array of words
10. Check if the current word is a property on the `wordFrequencies` object
11. Increment the value of the current word property on the `wordFrequencies` object
12. else
13. Set the value of the current word property on the `wordFrequencies` object to 1 because it has not already been set
14. End of else
15. End of loop
16. Set `currentMaxKey` to the first key on `wordFrequencies`
17. set `currentMaxCount` to the count for the value of the `currentMaxKey` property on `wordFrequencies`
18. Whitespace
19. Loop through the keys of the `wordFrequency` object. They current key is accessed via the `word` variable
20. Check if the `word` property on the `wordFrequencies` object is greater than the `currentMaxCount`
21. Set the `currentMaxKey` to the word as it is the most frequently occuring word we have found
22. Set the `currentMaxCount` to the `word` property on the `wordFrequencies` object as it is now the word with the most occurances
23. end of if
24. end of for loop
25. return the `currentMaxKey`
26. end of function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment