diff --git a/04-arithmetic/mediana.js b/04-arithmetic/mediana.js new file mode 100644 index 0000000..5c0cd1d --- /dev/null +++ b/04-arithmetic/mediana.js @@ -0,0 +1,30 @@ +const NUMBERS = [100, 200, 500, 400000000]; + +const calcularMediaAritmetica = (arr) => + arr.reduce( + (valorAcumulado = 0, nuevoElemento) => valorAcumulado + nuevoElemento + ) / arr.length; + +const mitadLista1 = NUMBERS.length / 2; + +function isEven(n) { + if (!n % 2 === 0) { + return true; + } + return false; +} + +let mediana; + +if (isEven(NUMBERS.length)) { + const elemento1 = NUMBERS[mitadLista1 - 1]; + const elemento2 = NUMBERS[mitadLista1]; + + const promedioElemento1y2 = calcularMediaAritmetica([elemento1, elemento2]); + + mediana = promedioElemento1y2; +} else { + mediana = NUMBERS[mitadLista1]; +} + +console.log(mediana); diff --git a/04-arithmetic/mode.js b/04-arithmetic/mode.js new file mode 100644 index 0000000..7ca06e7 --- /dev/null +++ b/04-arithmetic/mode.js @@ -0,0 +1,11 @@ +const NUMBERS = [2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 5, 5, 5, 5, 9]; + +function mode(arr){ + return arr.sort((a,b) => + arr.filter(v => v===a).length + - arr.filter(v => v===b).length + ).pop(); +} + + +console.log(mode(NUMBERS)); diff --git a/05-salary/analysis.js b/05-salary/analysis.js new file mode 100644 index 0000000..92c88a3 --- /dev/null +++ b/05-salary/analysis.js @@ -0,0 +1,39 @@ +const esPar = (n) => n % 2 === 0; + +const calcularMediaAritmetica = (lista) => + lista.reduce( + (valorAcumulado = 0, nuevoElemento) => valorAcumulado + nuevoElemento + ) / lista.length; + +function medianaSalarios(arr) { + const mitad = arr.length / 2; + + if (esPar(arr.length)) { + const personitaMitad1 = arr[mitad - 1]; + const personitaMitad2 = arr[mitad]; + + const mediana = calcularMediaAritmetica([personitaMitad1, personitaMitad2]); + return mediana; + } + return arr[mitad]; +} + +const salarios = colombia.map((personita) => personita.salary); + +const salariosColSorted = salarios.sort( + (salaryA, salaryB) => salaryA - salaryB +); + +const medianaGeneral = medianaSalarios(salariosColSorted); + +const spliceStart = (salariosColSorted.length * 90) / 100; +const spliceCount = salariosColSorted.length - spliceStart; + +const salaryTop10 = salariosColSorted.splice(spliceStart, spliceCount); + +const medianaTop10 = medianaSalarios(salaryTop10); + +console.log({ + medianaGeneralCol: medianaGeneral, + medianaTop10Col: medianaTop10, +}); diff --git a/05-salary/index.html b/05-salary/index.html new file mode 100644 index 0000000..9c44e52 --- /dev/null +++ b/05-salary/index.html @@ -0,0 +1,17 @@ + + +
+ + + + + +