Skip to content

Commit ba1bc62

Browse files
committed
Modified to handle inherent property cases and to remove empty spaces.
1 parent 7ef1f9c commit ba1bc62

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

Sprint-2/stretch/count-words.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ function countWords(string) {
3333
throw new Error("Input should be a string");
3434
}
3535
if (string === "") return {};
36-
const counts = {};
36+
const counts = Object.create(null) // prevents inherent properties.
3737

3838
// Remove punctuation and lowercase everything
3939

4040
const cleanText = string.replace(/[.,!?]/g, "").toLowerCase();
41-
const words = cleanText.split(" ");
41+
const words = cleanText.split(" ").filter(Boolean); // removes empty strings
4242

4343
// Count word frequencies
4444
for (const word of words) {
@@ -52,5 +52,9 @@ function countWords(string) {
5252
}
5353

5454
console.log(countWords("Hello!, my friend, You and me, and you."));
55+
console.log(countWords("constructor constructor"));
56+
console.log(countWords(" Hello World "));
5557

5658
// In count-words.js function countWords implemented.
59+
60+
// Modified to handle inherent property cases and to remove empty spaces.

0 commit comments

Comments
 (0)