w3resource

JavaScript: Find the most frequent word in a string

JavaScript String: Exercise-59 with Solution

Write a JavaScript program to find the most frequent word in a given string.
Test Data:
("The quick brown fox jumps over the lazy dog") -> "the"
("Python is a high-level, general-purpose programming language.") -> "python"
(" It was the same man, she was sure of it. It's always the same, Chauncey.") -> "was"
(12321) -> "It must be a string."

Sample Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to find the most frequent word in a string</title>
</head>
<body>

</body>
</html>

JavaScript Code:

const test = (text) => { 
   if (text.length === 0) 
    {
    return 'String should not be empty!'
   }
    if (typeof text !== 'string')
     {
       return 'It must be a string.'
     }
   const data = text.split(' ')
  if (data.length < 2) {
    return data[0]
  }
   const words = text.split(' ')
  if (words.length < 2) {
    return words[0]
  }
  const temp = {}
  words.forEach(word => {
    temp[word.toLocaleLowerCase()] = temp[word.toLocaleLowerCase()] + 1 || 1
  })
  const max = Object.keys(temp).reduce((n, word) => {
    if (temp[word] > n.count) 
    { 
      return { word, count: temp[word] } 
    } 
    else 
    { 
      return n 
    }
  }, { word: '', count: 0 })
  return max.word
}
console.log(test("The quick brown fox jumps over the lazy dog"))
console.log(test("Python is a high-level, general-purpose programming language."))
console.log(test(" It was the same man, she was sure of it. It's always the same, Chauncey."))
console.log(test(12321))

Sample Output:

the
python
was
It must be a string.

Flowchart:

Flowchart: JavaScript: find the most frequent word in a string

Live Demo:

See the Pen javascript-string-exercise-59 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Find the most frequent character in a string.
Next: Reverse words in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

function and arguments

const person = {
  name: 'Lydia Hallie',
  hobbies: ['coding'],
};

function addHobby(hobby, hobbies = person.hobbies) {
  hobbies.push(hobby);
  return hobbies;
}

addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);

console.log(person.hobbies);

The addHobby function receives two arguments, hobby and hobbies with the default value of the hobbies array on the person object.
First, we invoke the addHobby function, and pass "running" as the value for hobby and an empty array as the value for hobbies. Since we pass an empty array as the value for y, "running" gets added to this empty array.
Then, we invoke the addHobby function, and pass "dancing" as the value for hobby. We didn't pass a value for hobbies, so it gets the default value, the hobbies property on the person object. We push the hobby dancing to the person.hobbies array.
Last, we invoke the addHobby function, and pass "bdaking" as the value for hobby, and the person.hobbies array as the value for hobbies. We push the hobby baking to the person.hobbies array.
After pushing dancing and baking, the value of person.hobbies is ["coding", "dancing", "baking"]

Ref: https://bit.ly/2Hcpkm6