-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.higher_order_functions.js
More file actions
56 lines (46 loc) · 1.43 KB
/
13.higher_order_functions.js
File metadata and controls
56 lines (46 loc) · 1.43 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
53
54
55
56
// A function which takes a function as an argument or returns a function from it is known as the higher order function
function x(){
console.log("Nameste");
}
function y(x){
x();
}
// Here x is the callback function while y is the higher order function
// Significance is to reduce the repeatitive code
/*
Suppose you have to calculate the area,diameter,circumference of the circles given the radius of those circles
*/
let radius=[2,4,6,8,10];
// One way to find area will be
// const area=function(radius){
// const output=[];
// for(let i=0;i<radius.length;i++){
// output.push(Math.PI*radius[i]*radius[i]);
// }
// return output;
// }
// console.log(area(radius));
// Another optimized way to optimize the code
const area=function(radius){
return Math.PI*radius*radius;
}
const circumference=function(radius){
return 2*Math.PI*radius;
}
const diameter=function(radius){
return 2*radius;
}
const calculate=function(radius,area){ //Here you can change the functions to get the desired output
const output=[];
for(let i=0;i<radius.length;i++){
output.push(area(radius[i]));
}
return output;
}
console.log(calculate(radius,area));
// Now just see this calculate function is similar to the map
console.log(radius.map(area));
/*
If you do something like this Array.prototype.calculate=.....
The calculate function will be available over all the arrays ie radius.calculate(area);
*/