Guest User

Untitled

a guest
Jan 7th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. The basic purpose of this algorithm is to determine what word is used most commonly in the text submitted. We create an empty object -
  2. wordFrequencies. We iterate through each word in the text. If the word has not been encountered yet, we create a new key-value pair for it
  3. in this object, and sets its value to one. If the word has been come across already and we've already created a key-value pair, we increase
  4. its count by one. So the mostFrequentWord function is counting how many times each word is used in the text.
  5.  
  6. CurrentMaxKey becomes the first array item in the Object.keys array that displays all of the keys. currentMaxCount is the object key
  7. that currentMaxKey is set to. We start at index 0, as we're going to iterate through and compare each one.
  8.  
  9. We iterate through the wordFrequencies object using a for-in loop. As we iterate through, we
  10. check to see if this key-property pair has a higher numeric value than the current one (we start with the first one in the array as the one to
  11. compare to). If the current key-value pair has a higher value, we then set the key to currentMaxKey and the number to currentMaxCount.
  12. After going through each item in the array, we will have the highest key-value numeric value assigned to these variables, thus figuring out
  13. which word is used most. After the loop, we return the currentMaxKey, which is the word used most frequently.
Add Comment
Please, Sign In to add comment