I am trying to do word frequency and I have something working as far as printing out the word frequency. I now want to sort the output in Ascending Order.
var paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";
un_puncutated_paragraph = paragraph.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
let wordsHolder = un_puncutated_paragraph.split(' ');
let frequencyMap = {};
wordsHolder.forEach((word) => {
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
});
Also, could someone explain this logic a bit better, not entirely sure what is going on?
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
Thanks!