Skip to content
Open
Changes from 1 commit
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
60 changes: 42 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,44 @@ Words only consist of letters, never apostrophes or other punctuation symbols.
- identify most frequent word and return
*/

var nDictionary = Object.create(null);
function getWordsWithHighestFreq(paragraphArr) {
var frequencyDict = new Object();

function setDictionary(index, value) {
nDictionary[index] = value;
}

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

getDictWithFrequencies(paragraphArr, bannedWordsArr) {
var frequencyDict = new Object();
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;
}

/**
Expand All @@ -35,18 +61,16 @@ Words only consist of letters, never apostrophes or other punctuation symbols.
*/
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(" ");

var bannedArray = [];
bannedArray.push(banned);
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
getWordWithHighestFreq(wordsInPargrphArr,bannedArray);

const mostCommonWord = getWordsWithHighestFreq(wordsInPargrphArr);
return mostCommonWord.values();

console.log(noPunctuationParagraph);
console.log(wordsInPargrphArr)
console.log(bannedArray);
};

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