punto 8: funcion para maximo minimo y promedio con array
This commit is contained in:
@@ -49,3 +49,47 @@ function presentarse(nombre, edad) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(presentarse("Sergio", 33));
|
console.log(presentarse("Sergio", 33));
|
||||||
|
// Punto 8
|
||||||
|
const numeros = [1, 3, 8, 2, 18, 6];
|
||||||
|
|
||||||
|
// máximo
|
||||||
|
function obtenerMaximo(arr) {
|
||||||
|
let max = arr[0];
|
||||||
|
|
||||||
|
for (let num of arr) {
|
||||||
|
if (num > max) {
|
||||||
|
max = num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mínimo
|
||||||
|
function obtenerMinimo(arr) {
|
||||||
|
let min = arr[0];
|
||||||
|
|
||||||
|
for (let num of arr) {
|
||||||
|
if (num < min) {
|
||||||
|
min = num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
// promedio
|
||||||
|
function obtenerPromedio(arr) {
|
||||||
|
let suma = 0;
|
||||||
|
|
||||||
|
for (let num of arr) {
|
||||||
|
suma += num;
|
||||||
|
}
|
||||||
|
|
||||||
|
return suma / arr.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mostrar resultados
|
||||||
|
console.log("Máximo:", obtenerMaximo(numeros));
|
||||||
|
console.log("Mínimo:", obtenerMinimo(numeros));
|
||||||
|
console.log("Promedio:", obtenerPromedio(numeros));
|
||||||
Reference in New Issue
Block a user