Skip to content

Commit 45f6971

Browse files
authored
Merge pull request mouredev#4511 from bernatcs/main
#21-24 - JavaScript
2 parents 8d021d7 + 014edb0 commit 45f6971

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ** EJERCICIO
2+
3+
const readline = require('node:readline');
4+
5+
function numeroAnos(callback) {
6+
const rl = readline.createInterface({
7+
input: process.stdin,
8+
output: process.stdout
9+
});
10+
11+
rl.question('Por favor ingresa tu edad: ', (edad) => {
12+
callback(edad);
13+
rl.close();
14+
});
15+
}
16+
17+
function cuandoNaciste(edad) {
18+
let actual = new Date();
19+
let anoNacimiento = actual.getFullYear() - edad;
20+
console.log('Naciste en el: ' + anoNacimiento + ' o ' + (anoNacimiento - 1));
21+
}
22+
23+
// numeroAnos(cuandoNaciste)
24+
25+
26+
// ** DIFICULTAD EXTRA ** ------------------------------------------------------------------------------------------------------------------------------------------------
27+
28+
function procesaPedido(nombrePlato, callbackConfirmacion, callbackListo, callbackEntrega) {
29+
console.log(`Iniciando pedido para ${nombrePlato}`);
30+
setTimeout(() => {
31+
callbackConfirmacion(nombrePlato);
32+
setTimeout(() => {
33+
callbackListo(nombrePlato);
34+
setTimeout(() => {
35+
callbackEntrega(nombrePlato);
36+
}, Math.random() * 9000 + 1000);
37+
}, Math.random() * 9000 + 1000);
38+
}, Math.random() * 9000 + 1000);
39+
}
40+
41+
procesaPedido(
42+
'Pizza Margarita',
43+
(plato) => {
44+
console.log(`Confirmación recibida para ${plato}.`);
45+
},
46+
(plato) => {
47+
console.log(`${plato} está listo para ser entregado.`);
48+
},
49+
(plato) => {
50+
console.log(`${plato} ha sido entregado al cliente.`);
51+
}
52+
);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ** EJERCICIO
2+
3+
function ordenSuperior(comprobar, funcion) {
4+
if (comprobar){
5+
funcion()
6+
} else {
7+
console.log('No hay nada que decir')
8+
}
9+
}
10+
11+
function decirHola(){
12+
console.log('Hola')
13+
}
14+
15+
ordenSuperior('Sí que hay algo', decirHola)
16+
17+
// ** DIFICULTAD EXTRA ** -------------------------------------------------------------------------------------------------------------------------------
18+
19+
// const estudiantes = [
20+
// { nombre: 'Estudiante1', fechaNacimiento: '2000-04-22', notas: [7, 8, 9, 8, 7, 9, 8] },
21+
// { nombre: 'Estudiante2', fechaNacimiento: '1999-08-15', notas: [6, 7, 8, 7, 8, 9, 7] },
22+
// { nombre: 'Estudiante3', fechaNacimiento: '2001-12-03', notas: [8, 9, 9, 8, 9, 8, 9] },
23+
// { nombre: 'Estudiante4', fechaNacimiento: '2002-02-27', notas: [7, 8, 7, 8, 7, 8, 7] },
24+
// { nombre: 'Estudiante5', fechaNacimiento: '2000-11-10', notas: [9, 8, 8, 9, 8, 8, 9] },
25+
// { nombre: 'Estudiante6', fechaNacimiento: '1998-05-30', notas: [6, 7, 7, 6, 7, 7, 6] }
26+
// ];
27+
28+
29+
// // Promedio calificaciones: Obtiene una lista de estudiantes por nombre y promedio de sus calificaciones.
30+
// const obtenerPromedio = notas => notas.reduce((a, b) => a + b, 0) / notas.length;
31+
32+
// const promedios = estudiantes.map(estudiante => ({
33+
// nombre: estudiante.nombre,
34+
// promedio: obtenerPromedio(estudiante.notas)
35+
// }));
36+
37+
// console.log("Promedios:", promedios);
38+
39+
// // Mejores estudiantes: Obtiene una lista con el nombre de los estudiantes que tienen calificaciones con un 9 o más de promedio.
40+
// const mejoresEstudiantes = promedios
41+
// .filter(estudiante => estudiante.promedio >= 9)
42+
// .map(estudiante => estudiante.nombre);
43+
44+
// console.log("Mejores estudiantes:", mejoresEstudiantes);
45+
46+
// // Nacimiento: Obtiene una lista de estudiantes ordenada desde el más joven.
47+
// const estudiantesOrdenadosPorNacimiento = estudiantes
48+
// .slice()
49+
// .sort((a, b) => new Date(b.fechaNacimiento) - new Date(a.fechaNacimiento));
50+
51+
// console.log("Estudiantes ordenados por nacimiento (de más joven a más viejo):", estudiantesOrdenadosPorNacimiento.map(est => est.nombre));
52+
53+
// // Mayor calificación: Obtiene la calificación más alta de entre todas las de los alumnos.
54+
// const mayorCalificacion = Math.max(...estudiantes.flatMap(estudiante => estudiante.notas));
55+
56+
// console.log("Mayor calificación:", mayorCalificacion);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ** EJERCICIO
2+
3+
class Singleton {
4+
constructor() {
5+
if (Singleton.instance) {
6+
return Singleton.instance;
7+
}
8+
this.data = "Información del Singleton"; // Propiedades de la instancia
9+
Singleton.instance = this; // Guardar la instancia
10+
Object.freeze(this); // Congelar la instancia para evitar modificaciones
11+
}
12+
}
13+
14+
const instance = new Singleton();
15+
const instance2 = new Singleton()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ** EJERCICIO
2+
3+
function timeLogger(targetFunction) {
4+
return function(...args) {
5+
console.time('Execution Time');
6+
const result = targetFunction.apply(this, args);
7+
console.timeEnd('Execution Time');
8+
return result;
9+
};
10+
}
11+
12+
function sayHello(name) { // Función original
13+
console.log(`Hola, ${name}!`);
14+
}
15+
16+
const timedSayHello = timeLogger(sayHello); // Decorar la función
17+
18+
//timedSayHello('Pepe'); // Llamar a la función decorada
19+
20+
// ** DIFICULTAD EXTRA ** ------------------------------------------------------------------------------------------------------------------------------------------------
21+
22+
23+
function timeCounter(targetFunction) {
24+
let functionCount = 0
25+
26+
return function(...args) {
27+
const result = targetFunction.apply(this, args);
28+
functionCount++
29+
console.log('La función sayHello se ha ejecutado ' + functionCount + ' veces.')
30+
};
31+
}
32+
function sayHello(name) { // Función original
33+
console.log(`Hola, ${name}!`);
34+
}
35+
const countedSayHello = timeCounter(sayHello)
36+
37+
countedSayHello('Juan')
38+
countedSayHello('María')
39+
countedSayHello('Julián')

0 commit comments

Comments
 (0)