Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
/**
Purpose: write one or more methods that returns the most common words
The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols,
and even if it is a proper noun.)
Paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
There are no hyphens or hyphenated words.
Words only consist of letters, never apostrophes or other punctuation symbols.
*/

/**
Plan Of Action:
- remove punctuation and make lowercase
- remove banned word from paragraph
- identify most frequent word and return
*/

function getWordsWithHighestFreq(paragraphArr) {
var frequencyDict = new Object();

//Adds words from paragraph into a dictionary
for (var i = 0; i < paragraphArr.length; i++) {
var key = paragraphArr[i];

if(key in frequencyDict) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a space between 'if' and first (
Same for 'for' loops (line 21 looks good)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, haven't seen (propertyName in objectName) used before--that's cool. Most of the time I tend to see objectName.hasOwnProperty(propName), which makes sure that the property is not on the object's prototype

value = frequencyDict[key];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you should have declare 'value' with var or let here, unlike Object.entries which provides [key, value].
Without a var, it becomes an implicit global level variable

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure just in case I'm missing something like Object.entries

frequencyDict[key] = value + 1;
} else {
frequencyDict[key] = 1;
}
}
console.log("The values in the dictioanry are the following: ", frequencyDict);


//Identify the number of the maximum occurance of a word
var max = 0;

for(const [key, value] of Object.entries(frequencyDict)) {
if(value > max) {
max = value;
}
}
console.log("The maximum occurance of a word is: ", 2)

//Parse dictionary to retrieve words of highest count
var wordsOfHighFrequency = []

for(const [key, value] of Object.entries(frequencyDict)) {
if(value == max) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===

wordsOfHighFrequency.push(key);
}
}
console.log("The most common words are", wordsOfHighFrequency);

return wordsOfHighFrequency;
}

/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
const mostCommonWord = (paragraph, banned) => {
var noPunctuationParagraph = paragraph.toLowerCase().replace(/(!|;|'|,|\.|\?)/g,"").trim();
console.log("The paragraph written in lowercase and without punctuation is the following: " ,noPunctuationParagraph);

var wordsInPargrphArr = noPunctuationParagraph.split(" ");
wordsInPargrphArr = wordsInPargrphArr.filter(word => word != banned);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use !== instead of != (and same for ===) in Javascript, unless you're specifically looking to coerce values. I know the udemy course has more info somewhere in the first half

console.log("The array of words in paragraph without the banned word is the following: ", wordsInPargrphArr);

//method retrieves the word with the highest frequency
const mostCommonWord = getWordsWithHighestFreq(wordsInPargrphArr);
return mostCommonWord.values();

};

};
mostCommonWord("THE CAT IS HERE, and dog is here?", "cat");