Skip to content

Commit c4a0b7d

Browse files
committed
#24 - JavaScript
1 parent ed4bbff commit c4a0b7d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
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)