-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithHOF.js
More file actions
52 lines (40 loc) · 1.14 KB
/
WithHOF.js
File metadata and controls
52 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let radius = [2, 4, 6, 8];
const area = (radius) => {
return Math.PI * radius * radius;
};
const circumference = (radius) => {
return 2 * Math.PI * radius;
};
const diameter = (radius) => {
return 2 * radius;
};
// Function taking a function
const calculate = (logicFn, radius) => {
let output = [];
for (let i = 0; i < radius.length; i++) {
output.push(logicFn(radius[i]));
}
return output;
};
console.log(calculate(area, radius));
console.log(calculate(circumference, radius));
console.log(calculate(diameter, radius));
console.log("--------------------------");
// Function returning a function
const greet = (message) => {
return function (user) {
return `${message}, ${user}`;
};
};
const sayHello = greet("Hello");
console.log(sayHello("Yeju"));
console.log("--------------------------");
function createLogger(prefix) {
return function (message) {
console.log(`[${prefix}] ${message}`);
};
}
const infoLogger = createLogger("INFO");
const errorLogger = createLogger("ERROR");
infoLogger("Server started"); // Output: [INFO] Server started
errorLogger("Something went wrong"); // Output: [ERROR] Something went wrong