forked from juandc/platzi-curso-practico-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromedio.js
47 lines (43 loc) · 1.67 KB
/
promedio.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
let listaUsario = [];
const numeroDatos = () =>
parseInt(document.getElementById("numeroDatos").value);
const aggInput = () => {
// Eliminar todos los elementos hijos del padre;
var elemento = document.getElementById("aggInputs");
while (elemento.firstChild) {
elemento.removeChild(elemento.firstChild);
};
// Quitar respuesta
const resultado = document.getElementById("resultado");
resultado.innerText = "";
// Colocar los input
let valorNumeroDatos = numeroDatos();
for (let i = 1; i < (valorNumeroDatos + 1); i++ ) {
var direccion = `<label for="${i}">Dato ${i}: </label>
<input id="${i}" type="number"/>`;
let input1 = document.getElementById("aggInputs");
input1.insertAdjacentHTML('beforeend', direccion);
}
let button = document.getElementById("aggInputs");
button.insertAdjacentHTML('beforeend',
'<button type="button" onclick="calcularMediaAritmetica()">Calcular</button>');
}
const calcularMediaAritmetica = () => {
listaUsario = [];
datosUsuario();
const sumaLista = listaUsario.reduce(
(valorAcumulado = 0, nuevoElemento) => valorAcumulado + nuevoElemento);
const promedioLista = sumaLista / listaUsario.length;
const resultado = document.getElementById("resultado");
resultado.innerText = "El promedio es: " + promedioLista;
}
const datosUsuario = () => {
let valorNumeroDatos = numeroDatos();
// Trae los datos a JS y los agrega a la lista
for (let i = 1; i < (valorNumeroDatos + 1); i++){
let id = i;
let dato = document.getElementById(id);
let valueDato = parseInt(dato.value);
listaUsario.push(valueDato);
}
}