-
Notifications
You must be signed in to change notification settings - Fork 0
/
analisis.js
50 lines (36 loc) · 1.22 KB
/
analisis.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
// Helpers or utils
function esPar(numero) {
return numero % 2 === 0;
}
function calcMedian(data) {
let median;
const middle = parseInt(data.length / 2);
if (esPar(data.length)) {
const firstElement = data[middle - 1];
const secondElement = data[middle];
median = calcArithmeticMean([firstElement, secondElement]);
} else {
median = data[middle];
}
return median;
}
function calcArithmeticMean(data) {
const sumOfData = data.reduce((prev = 0, curr) => prev + curr);
const arithmeticMean = sumOfData / data.length;
return arithmeticMean;
}
// Calculadora de mediana
// General median
const salariosCol = colombia.map((persona) => persona.salary);
const salariosColSorted = salariosCol.sort((prev, curr) => prev - curr);
const colombianGeneralMedian = calcMedian(salariosColSorted);
// Top 10% median
const sliceStart = (salariosColSorted.length * 90) / 100;
const sliceCount = salariosColSorted.length - sliceStart;
const colombianTop10Salaries = salariosColSorted.splice(sliceStart, sliceCount);
const colombianTop10Median = calcMedian(colombianTop10Salaries);
console.log(
`Mediana salarial general: ${colombianGeneralMedian}`,
`\n`,
`Colombian top 10% median: ${colombianTop10Median}`
);