Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript: count words

function countWords(str) {
  let counts = {};
  let words = str.split(' ');
  for (let word of words) {
    if (word.length > 0) {
      if (!(word in counts)) {
        counts[word] = 0;
      }
      counts[word] += 1;
    }
  }
  return counts;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #count #words
ADD COMMENT
Topic
Name
7+2 =