Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) {
- // NB: `.filter(Boolean)` removes any falsy items from an array
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); // This is a return statement signifying to retuern rawstring in lower case and split the string into an array of substrings, and returns the new array. Make a new one that contains only items that a filtering function returns true. Sort the original array and make a new copy.
- }
- function mostFrequentWord(text) {
- // A function mostFrequentWord is declared
- let words = getTokens(text);
- let wordFrequencies = {};
- // this is signifying to let wordfrequencies = the value based on the conditional satement.
- for (let i = 0; i <= words.length; i++) {
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- } else {
- wordFrequencies[words[i]] = 1;
- }
- }
- //The code above will log the numbers 1 to 0 to the console. It says beginning at i= 0, log the value of i. After logging i, if i is less than or equal to the value of words.length, increment i by one, and then repeat the process.
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- //let current MaxKey = the object in the word frequencies.
- let currentMaxCount = wordFrequencies[currentMaxKey];
- for (let word in wordFrequencies) {
- if (wordFrequencies[word] > currentMaxCount) {
- //If the word in wordFrequencies is greater than the currentMaxCount return the currentMaxKey.
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- }
- }
- return currentMaxKey;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement