File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
5454console . 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.
You can’t perform that action at this time.
0 commit comments