-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhigherorderfunc.js
61 lines (48 loc) · 1.32 KB
/
higherorderfunc.js
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
53
54
55
56
57
58
59
60
//Higher Order Function :✅
//A function which accept a function as a parameter
//or Return a function or Both
//for ex : ForEach Method alwaays takes another function inside it
//So forEach is a higher order function
function f(val) {
val();
return function g() { console.log("Ojha"); };
}
var gres = f(function fx() {
console.log("Kirtti");
});
gres();
//or ex:
var arr = [1, 2, 3, 4, 5];
arr.forEach(function () { })
// function findArea(radius) {
// const res =[];
// for(let i=0;i < radius.length; i++)
// {
// res.push(Math.PI*radius[i]*radius[i]);
// }
// return res;
// }
// console.log(findArea(radius));
//and similarly create for find diameter and circumference
//That would be not a good programming skill to repeat the code
//so the optimized and good programming skill would be
const radius = [3, 4, 5, 2];
const area = function (radius) {
return Math.PI * radius * radius;
}
const diameter = function (radius) {
return 2 * radius;
}
const circumference = function (radius) {
return 2*Math.PI*radius;
}
function calculate(radius, logic) {
const output = [];
for (let i = 0; i < radius.length; i++) {
output.push(logic(radius[i]));
}
return output;
}
console.log(calculate(radius, area));
console.log(calculate(radius, circumference));
console.log(calculate(radius, diameter));