Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. function modalWord(words) {
  2. // words = words.map(function(s) { return s.toLowerCase(); }); // nomalize
  3. var counter = {};
  4. words.forEach(function(str){
  5. if(counter.hasOwnProperty(str)) {
  6. counter[str]++;
  7. } else {
  8. counter[str] = 1;
  9. }
  10. });
  11.  
  12. var maxCount = 0;
  13. var modal;
  14.  
  15. for (var keyStr in counter) {
  16. if (maxCount === 0) {
  17. modal = keyStr;
  18. maxCount = counter[keyStr];
  19. } else {
  20. if (counter[keyStr] > maxCount) {
  21. modal = keyStr;
  22. maxCount = counter[keyStr];
  23. }
  24. }
  25. }
  26. return modal;
  27. }
Add Comment
Please, Sign In to add comment