find the highest repeating word from a given file in JavaScript

JavaScript
x
24
var fs = require('fs');
var file = fs.readFileSync('file.txt', 'utf8');
var words = file.split(/\s+/);
var wordCounts = {};
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (wordCounts[word]) {
wordCounts[word]++;
} else {
wordCounts[word] = 1;
}
}
//console.log(words);
//console.log(wordCounts);
//console.log(Object.keys(wordCounts));
//console.log(Object.values(wordCounts));
🤖 Code Explanation
This code is reading a file, splitting it into words, and then counting how many times each word appears. It is then printing out the most common word.

More problems solved in JavaScript




















