I’m new to programming and I wanted to make a word frequency counter where it finds the most frequent word and has it pop up as an alert. Although I have read some articles I can’t seem to find a proper solution. For example, I want my alert to say the most frequent word in the text area is "Hello" 2. Any help will be appreciated.
function countWords() {
let input = document.getElementById("input").value;
var counter;
for(var i = 0; i < input.length; i++) {
if(input[i] == input[i+1]){
counter = counter + 1;
}
}
alert(counter);
}
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="main.js"></script>
</head>
<body>
<textarea id="input" rows="30" cols="30"></textarea><br>
<button type="button" onclick="countWords()">Count Repeated Words</button>
</body>
</html>
2
Answers
Here’s a method to do everything you’re looking for, broken down with comments:
Here’s a solution, tried to comment each step: