Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // changed noting methodology to writing notes within the code with comments. Went through each line and added a note saying what it does, and also did a variety of testing with console.log(); on things I was unsure about. Tested the functions at the end and found it working.
- function getTokens(rawString) {
- // a named function called getTokens with a single argument (rawString)
- // NB: `.filter(Boolean)` removes any falsy items from an array
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- //any text used in the rawString argument is changed to lowercase. .toLowercase() is a built in method in JS
- // .split() is a built in method that is used here to separate each word that might be separated by white space or special characters.
- // .filter(Boolean) is a built in method removes falsy entries from the array and .sort() with no argument will sort words alphabetically
- }
- function mostFrequentWord(text) {
- // establishes a named function called mostFrequentWord with the single argument (text)
- let words = getTokens(text);
- // establishes the variable called words and calls the getTokens(text) function explained earlier
- let wordFrequencies = {};
- // establishes an empty object called wordFrequencies
- for (let i = 0; i <= words.length; i++) {
- // a for loop using the standard 3 arguments: let i = 0; is the initialized counter variable, i <= words.length; is the condition by which it is told to continue; i++ dictates how the counter is to be incremented, so each time the loop happens i increases by 1.
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- // if statement that if the current word (starting with words[0]) is in wordFrequencies already, then increment 1 to that number.
- } else {
- wordFrequencies[words[i]] = 1;
- //else statement used so that if words[i] is not in wordFrequncies yet, it's count is set to 1
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- // This line uses the built in Object.keys() to go through the wordFrequencies object and create an array of all of the keys in it. It creates a variable called currentMaxKey at [0] to start at the beginning.
- console.log(currentMaxKey);
- // This returns 'a' because the text is sorted in alphabetical order
- let currentMaxCount = wordFrequencies[currentMaxKey];
- // a variable named currentMaxCount which accesses the wordFrequencies object and calls whichever item number is aligned to the number of currentMaxKey
- for (let word in wordFrequencies) {
- // creates a for loop that is used to iterate through all of the words in wordFrequencies
- if (wordFrequencies[word] > currentMaxCount) {
- // if statement that looks for any wordFrequencies that are greater than the currentMaxCount variable established earlier
- currentMaxKey = word;
- // if it finds one, then currentMaxKey is changed to that new word
- currentMaxCount = wordFrequencies[word];
- // sets the new currentMaxCount to the number of uses of that word
- }
- console.log(currentMaxKey);
- console.log(currentMaxCount);
- }
- return currentMaxKey;
- }
- console.log(mostFrequentWord('I am testing this function to make sure it is working. Testing a function like this can be rewarding because I like testing things. Testing is one of the best things you can do'));
- // printed => testing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement