Skip to content

Commit 19c8bd9

Browse files
committed
Culminado taller 3 de promedio, mediana y moda
1 parent 2f3fbcf commit 19c8bd9

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

mediana.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Mediana</title>
8+
</head>
9+
<body>
10+
<header>
11+
<h1>Calculando la mediana.</h1>
12+
</header>
13+
14+
<script src="./mediana.js"></script>
15+
</body>
16+
</html>

mediana.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const calculateMiddleArithmetic = (list) => {
2+
const reducer = (accumulator, currentValue) => accumulator + currentValue;
3+
4+
const sumList = list.reduce(reducer);
5+
6+
const averageList1 = sumList / list.length;
7+
return averageList1;
8+
}
9+
10+
11+
12+
const list1 = [
13+
100,
14+
300,
15+
11,
16+
40000,
17+
500,
18+
10000
19+
];
20+
21+
list1.sort(function(a, b) {
22+
return a - b;
23+
});
24+
25+
const isPar = (number) => {
26+
if (number % 2 === 0) {
27+
return true;
28+
} else {
29+
return false;
30+
}
31+
}
32+
33+
let middle;
34+
35+
const middleList1 = parseInt(list1.length / 2);
36+
37+
if (isPar(list1.length)) {
38+
const element1 = list1[middleList1 - 1];
39+
const element2 = list1[middleList1];
40+
41+
const element1_2 = calculateMiddleArithmetic([
42+
element1,
43+
element2
44+
]);
45+
46+
47+
middle = element1_2;
48+
49+
} else {
50+
middle = list1[middleList1];
51+
}

moda.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Moda</title>
8+
</head>
9+
<body>
10+
<header>
11+
<h1>
12+
Calculando la moda
13+
</h1>
14+
</header>
15+
16+
<script src="./moda.js"></script>
17+
</body>
18+
</html>

moda.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const list1 = [
2+
1,
3+
2,
4+
3,
5+
3,
6+
2,
7+
4,
8+
5,
9+
2,
10+
2,
11+
2,
12+
5,
13+
4,
14+
4,
15+
4
16+
]
17+
18+
const list1Count = {};
19+
20+
//Recorremos nuestro objeto para saber cuantas veces se repite cada valor.
21+
list1.map(
22+
function (element) {
23+
if (list1Count[element]) {
24+
list1Count[element] += 1;
25+
} else {
26+
list1Count[element] = 1;
27+
}
28+
}
29+
);
30+
31+
//Convertimos nuestro objeto list1Count en una array, para ordenarlo de menor a mayor.
32+
const list1Array = Object.entries(list1Count).sort(
33+
function (valueAcumulated, newValue) {
34+
return valueAcumulated[1] - newValue[1];
35+
}
36+
);
37+
38+
const moda = list1Array[list1Array.length - 1];
39+
40+
41+
//Otra forma mas corta
42+
const NUMBERS = [2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 5, 5, 5, 5, 9];
43+
44+
function mode(arr){
45+
return arr.sort((a,b) =>
46+
arr.filter(v => v===a).length - arr.filter(v => v===b).length)
47+
.pop();
48+
}
49+
50+
51+
console.log(mode(NUMBERS));

0 commit comments

Comments
 (0)