forked from Laboratoria/DEV013-text-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.js
81 lines (74 loc) · 2.78 KB
/
analyzer.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const obtenerNumeros = function(texto){
const number = texto.match(/\b\d+(\.\d+)?\b/g);
if (number === null){
return []
}
return number
}
const analyzer = {
getWordCount: (text) => {
//TODO: esta función debe retornar el recuento de palabras que se encuentran en el parámetro `text` de tipo `string`.
const word = text.trim().split(/\s+/g).length;
return word;
},
getCharacterCount: (text) => {
//TODO: esta función debe retornar el recuento de caracteres que se encuentran en el parámetro `text` de tipo `string`.
const character = text.length;
return character
},
getCharacterCountExcludingSpaces: (text) => {
//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`.
//Use expresiones regulares W llama a todo lo q no es un caracter de palabra y la g es global osea todo .length cantidad
const excludingSpaces = text.replace(/\W/g, "").length;
return excludingSpaces
},
getNumberCount: (text) => {
//TODO: esta función debe retornar cúantos números se encuentran en el parámetro `text` de tipo `string`.
// con el metodo match estoy buscando solo nos numeros (exp regular)
const arrayNumeros = obtenerNumeros(text)
const cantidadNumeros = arrayNumeros.length
return cantidadNumeros
// const number = text.match(/\b\d+(\.\d+)?\b/g)
// let numberCount = 0;
// if (number === null) {
// return 0
// } else {
// for (let i = 0; i < number.length; i++) {
// numberCount++;
// }
// }
// return numberCount
},
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 arrayNumeros = obtenerNumeros(text)
let numberSum = 0
for (let i = 0; i < arrayNumeros.length; i++) {
const item = arrayNumeros[i];
numberSum = numberSum + parseFloat(item);
}
return numberSum
// const number = text.match(/\b\d+(\.\d+)?\b/g)
// let numberSum = 0;
// if (number === null) {
// return 0
// } else {
// for (let i = 0; i < number.length; i++) {
// const item = number[i]
// numberSum = numberSum + parseFloat(item);
// }
// }
// return numberSum;
},
getAverageWordLength: (text) => {
//TODO: esta función debe retornar la longitud media de palabras que se encuentran en el parámetro `text` de tipo `string`.
const word = text.trim().split(/\s+/g)
let suma = 0
for (let i = 0; i < word.length; i++) {
suma += word[i].length;
}
const getAverageWordLength = suma / word.length;
return Math.round(getAverageWordLength*100)/100;
},
};
export default analyzer;