Advertisement
kstoyanov

03. Count Words in a Text

Sep 22nd, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function countWords(arr) {
  2.   const words = {};
  3.   const text = arr.shift();
  4.  
  5.   const arrOfWords = text.split(/\W+/g).filter(Boolean);
  6.   arrOfWords.forEach((str) => {
  7.     words[str] = (words[str] || 0) + 1;
  8.   });
  9.  
  10.  
  11.   console.log(JSON.stringify(words));
  12. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement