feat: implement max, min and average functions using loops

This commit is contained in:
2026-04-28 16:14:05 -03:00
parent 1358fb523b
commit 59b1d96f5b

View File

@@ -25,19 +25,19 @@ if (age >= 18) {
// Tercer ejercicio: // Tercer ejercicio:
const max = 20; const maximum = 20;
let n = 3; let n = 3;
const multiples = []; const multiples = [];
while (n < max) { while (n < maximum) {
multiples.push(n); multiples.push(n);
n +=3; n +=3;
} }
console.log(`Múltiples de 3 menores que ${max}:`, multiples); console.log(`Múltiples de 3 menores que ${maximum}:`, multiples);
// Cuarto ejercicio: // Cuarto ejercicio:
for (let i = 3; i < max; i += 3) { for (let i = 3; i < maximum; i += 3) {
console.log(i); console.log(i);
} }
@@ -52,4 +52,39 @@ for (const fruit of fruits) {
// Sexto ejercicio: // Sexto ejercicio:
const presentation = (a, b) => `Hola, mi nombre es ${a} y tengo ${b} años.`; const presentation = (a, b) => `Hola, mi nombre es ${a} y tengo ${b} años.`;
console.log(presentation(name, age)); console.log(presentation(name, age));
// Séptimo ejercicio:
const numbers = [1, 3, 6, 12, 24, 5, 2, 9, 15];
const maxValue = (arr) => {
let max = arr[0];
for (const num of arr) {
if (num > max) {
max = num;
}
}
return max;
};
console.log(`El valor máximo es: ${maxValue(numbers)}`);
const minValuer = (arr) => {
let min = arr[0];
for (const num of arr) {
if (num < min) {
min = num;
}
}
return min;
}
console.log(`El valor mínimo es: ${minValuer(numbers)}`);
const calculateAverage = (arr) => {
let sum = 0;
for (const num of arr) {
sum += num
}
return (sum / arr.length).toFixed(2);
}
console.log(`El promedio es: ${calculateAverage(numbers)}`);