-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextProcessor.js
More file actions
39 lines (33 loc) · 1.23 KB
/
textProcessor.js
File metadata and controls
39 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// a simple text analyzer that gives information on char count, word count, and the most common char
function analyzeText(text) {
// word count
const wordCount = text.split(/\s+/).filter(word => word.length > 0).length;
// char count (frequency) using hash maps
const charFrequency = {};
for (let char of text) {
if (char.match(/[a-z]/i)) { // Solo letras, ignoramos signos
const lowerChar = char.toLowerCase();
// Si existe suma 1, si no, inicializa en 1
charFrequency[lowerChar] = (charFrequency[lowerChar] || 0) + 1;
}
}
// search for the most common char
let maxCount = 0;
let mostCommonChar = ' ';
for (let char in charFrequency) {
// buscar el caracter mas repetido
if (charFrequency[char] > maxCount) {
maxCount = charFrequency[char];
mostCommonChar = char;
}
}
// return information in hash map so it can be formatted in json
return {
originalLength: text.length,
wordCount: wordCount,
mostFrequentChars: charFrequency,
mostCommonLetter: mostCommonChar
}
}
// export the function so other programs/scripts can use it
module.exports = {analyzeText};