Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) {
- // This function take a single argument 'rawString' which will have to be a string //
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- // It returns the string with all lowercased words using `.toLowerCase()` //
- // Then with `.string(/[ ,!.";:-]+/)` it splits the string into an array while removing all special characters and spaces //
- // NB: `.filter(Boolean)` removes any falsy items from an array
- // Finally `.sort()` organizes the remaining words in alphabetical order //
- }
- function mostFrequentWord(text) {
- // This function also takes a single argument 'text' which will also be a string //
- let words = getTokens(text);
- // The variable `words` is being defined as the function that was just described above with the argument changed to 'text' //
- let wordFrequencies = {};
- // The variable `wordFrequencies` is defined as an empty object for now //
- for (let i = 0; i < words.length; i++) {
- // This `for` loop starts at the first index of the array when i=0 //
- // Then it runs until the index is less than or equal to the number of items in the `words` array (`i <= words.length`) //
- // It adds 1 to the index each time the loop is run with i++ //
- // 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 //
- // To fix this, change the operator to '<' instead of '<=' //
- if (words[i] in wordFrequencies) {
- // This conditional takes every `words` index item and makes it a key in the object `wordFrequencies` //
- wordFrequencies[words[i]]++;
- // If the conditional is true, then it adds one count to the value of the key `words[i]` //
- } else {
- wordFrequencies[words[i]] = 1;
- // If it is false, the value of the key `words[i]` will be equal to 1 //
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- // The variable `currentMaxKey` is defined as the first key in the object `wordFrequencies` //
- let currentMaxCount = wordFrequencies[currentMaxKey];
- // The variable `currentMaxCount` is defined as the value of the `currentMaxKey` in the object `wordFrequencies` //
- for (let word in wordFrequencies) {
- // 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 //
- if (wordFrequencies[word] > currentMaxCount) {
- // This conditional compares the value of each key in `wordFrequencies` to the variable `currentMaxCount` //
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- // 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 //
- }
- }
- // This function will return the value of currentMaxKey when it is called which should be the word that appears most frequently //
- return currentMaxKey;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement