// Desafio 1, 2, 3 let listaGenerica = []; console.log(listaGenerica[0]);
let lenguajesDeProgramacion = ['JavaScript', 'C', 'C++', 'Kotlin', 'Python']; lenguajesDeProgramacion.push('Ruby', 'GoLang'); // Desafio 4 console.log(lenguajesDeProgramacion.length); // Desafio 5 function inverso () { lenguajesDeProgramacion.reverse(); console.log(lenguajesDeProgramacion); } inverso();
// Desafío 6 let numeros = ["2", "3", "1", "7", "9", "5", "6"]; //Metodo 1: Propio function promedio1 () { let suma = 0; for (let i = 0; i < numeros.length; i++) { const element = Number(numeros[i]);//Convertir cada elemento en numero suma += element; //Toma el elemento para sumarlo } return suma / numeros.length; } console.log(promedio1()); //Metodo 2: .reduce function promedio() { //Ejecucion de la funcion reductora: acc = toma el primer valor del índice, val = guarda el valor, Numero(val) = convierte 2 a 2, ac + Numero(val) = 0+2 = 2 let suma = numeros.reduce((acc, val) => acc + Number(val), 0);; return suma / numeros.length; } console.log(promedio());
// Desafío 7 function minMax() { let saveMax = -Infinity;//valor más pequeño posible let saveMin = Infinity;//el más grande posible
for (let i = 0; i < numeros.length; i++) {
const element = Number(numeros[i]);
if(element > saveMax){ //Al comparar cualquier numero con -Infinity siempre sera mayor
saveMax = element;
}
if(element < saveMin){//Al comparar cualquier numero con Infinity siempre sera menor
saveMin = element;
}
}
return { max: saveMax, min: saveMin };
} console.log(minMax());
// Desafío 9 function posicion(indice) { for (let i = 0; i < numeros.length; i++) { const element = Number(numeros [i]); if(element === indice){ return i; //El contador regresa el índice } } return -1; } let indice = parseInt(prompt("Ingrese el numero para buscarlo en la lista:")); console.log(posicion(indice)); // Imprime la posición o -1 si no se encuentra
// Desafío 10 let tamaño = parseInt(prompt("Ingrese el tamaño del arreglo:")); let listaUno = new Array(tamaño); let listaDos = new Array(tamaño);
for(let j = 0; j < tamaño; j++){
listaUno[j] = parseInt(prompt(Ingrese los numeros para el primer arreglo ${j + 1}:
));
listaDos[j] = parseInt(prompt(Ingrese los numeros para el segundo arreglo${j + 1}:
));
}
function sumaListas(lista){ sumaTotal = 0; if (listaUno.length !== listaDos.length) { throw new Error("Las listas deben tener el mismo tamaño"); } let resultado = [];
for (let i = 0; i < listaUno.length; i++) {
const element = listaUno[i] +listaDos[i];
console.log(`${listaUno[i]} + ${listaDos[i]} = ${element}`)
resultado.push(element);
sumaTotal += listaUno[i] +listaDos[i];
}
return resultado;
} console.log("La suma de los elementos correspondientes de los arrays es:", sumaListas(listaUno, listaDos),"El resultado total es: ", sumaTotal);
// Desafío 11 let tamañoN = parseInt(prompt("Ingresa el tamaño del arreglo: ")); let lista = new Array(tamañoN);
for (let j = 0; j < tamañoN; j++) {
lista[j] = parseInt(prompt(Ingrese los numeros para el arreglo:
));
}
function listaCuadrado() {
let cuadrado= 0;
for (let i = 0; i < lista.length; i++) {
const element = lista[i] * lista[i];
console.log(El cuadrado de ${lista[i]} es: ${element}
);
}
} listaCuadrado();