Skip to content

Commit bc3fcd4

Browse files
complete countwords
1 parent 46805c8 commit bc3fcd4

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

‎Sprint-2/stretch/count-words.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@
2626
2727
3. Order the results to find out which word is the most common in the input
2828
*/
29+
function countWords(text) {
30+
const cleanedText = text.toLowerCase().replace(/[.,!?]/g, "");
31+
32+
const words = cleanedText.split(" ").filter((word) => word !== "");
33+
34+
const wordCounts = {};
35+
36+
for (const word of words) {
37+
wordCounts[word] = (wordCounts[word] || 0) + 1;
38+
}
39+
40+
return wordCounts;
41+
}
42+
43+
module.exports = countWords;

0 commit comments

Comments
 (0)