forked from Laboratoria/DEV011-text-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.js
More file actions
55 lines (52 loc) · 2.47 KB
/
analyzer.js
File metadata and controls
55 lines (52 loc) · 2.47 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const analyzer = {
getWordCount: (text) => {
const textoAreaDividido = text.trim().split(" ");
return textoAreaDividido.length;
//agregar if ===0 porque cuenta como palabras signos de expresion (?!) y cuenta el primer espacio.
//TODO: esta función debe retornar el recuento de palabras que se encuentran en el parámetro `text` de tipo `string`.
},
getCharacterCount: (text) => {
return text.length;
//TODO: esta función debe retornar el recuento de caracteres que se encuentran en el parámetro `text` de tipo `string`.
},
getCharacterCountExcludingSpaces: (text) => {
const caracteresSinEspacios = text.trim().replace(/[\s,.{[!?;:()+]/g, ""); //expresión regular que se utiliza para buscar y reemplazar espacios en blanco en una cadena de texto.//
return caracteresSinEspacios.length;
//TODO: esta función debe retornar el recuento de caracteres excluyendo espacios y signos de puntuación que se encuentran en el parámetro `text` de tipo `string`.
},
getAverageWordLength: (text) => {
const arrayPalabras = text.trim().split(" ") //.trimmétodo que se utiliza para eliminar los espacios en blanco al principio y al final de una cadena de texto.
;
const palabrasLength = arrayPalabras.map(function (palabra) {
return palabra.length;
});
let sumPalabras = 0;
palabrasLength.map(function (palabrasLength) {
sumPalabras = sumPalabras + palabrasLength;
});
const resultado = sumPalabras / arrayPalabras.length;
return Number(resultado.toFixed(2));
//TODO: esta función debe retornar la longitud media de palabras que se encuentran en el parámetro `text` de tipo `string`.
// return LongitudMedia.length;
},
getNumberCount: (text) => {
let onlynumbers = text.match(/\b\d+(\.\d+)?\b/g); // secuencias de uno o más dígitos en una cadena de texto//
if (onlynumbers === null) {
onlynumbers = "";
}
return onlynumbers.length;
//TODO: esta función debe retornar cúantos números se encuentran en el parámetro `text` de tipo `string`.
},
getNumberSum: (text) => {
//TODO: esta función debe retornar la suma de todos los números que se encuentran en el parámetro `text` de tipo `string`.
const listaText = text.replace(/\s+/g, " ").trim().split(" ");
let sumNumber = 0;
for (let i = 0; i < listaText.length; i++) {
if (!isNaN(listaText[i])) {
sumNumber += Number(listaText[i]);
}
}
return sumNumber;
},
};
export default analyzer;