Skip to content

Commit 72d0b61

Browse files
committed
#24 - JavaScript
1 parent b2454d4 commit 72d0b61

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function greet(name) {
2+
return `Hello, ${name}!`;
3+
}
4+
5+
function logDecorator(fn) {
6+
return function(...args) {
7+
console.log(`Calling function: ${fn.name}`);
8+
const result = fn(...args);
9+
console.log(`Function ${fn.name} executed with result: ${result}`);
10+
return result;
11+
};
12+
}
13+
14+
const decoratedGreet = logDecorator(greet);
15+
console.log(decoratedGreet("Isaac"));
16+
17+
// Extra Exercise //
18+
function callCountDecorator(fn) {
19+
let count = 0;
20+
21+
return function(...args) {
22+
count++;
23+
console.log(`Function ${fn.name} has been called ${count} times`);
24+
return fn(...args);
25+
};
26+
}
27+
28+
const decoratedGreet2 = callCountDecorator(greet);
29+
console.log(decoratedGreet2("Hiel"));
30+
console.log(decoratedGreet2("Kurama"));
31+
console.log(decoratedGreet2("Yusuke"));
32+
console.log(decoratedGreet2("Kuwabara"));

0 commit comments

Comments
 (0)