Answers for "1. Given a phrase, count the occurrences of each word in that phrase."

0

1. Given a phrase, count the occurrences of each word in that phrase.

function count(str) {
  var obj = {};
  
  str.split(" ").forEach(function(el, i, arr) {
    obj[el] = obj[el] ? ++obj[el] : 1;
  });
  
  return obj;
}

console.log(count("olly olly in come free"));
Posted by: Guest on June-06-2021

Code answers related to "1. Given a phrase, count the occurrences of each word in that phrase."

Browse Popular Code Answers by Language