4
4
//for ex : ForEach Method alwaays takes another function inside it
5
5
//So forEach is a higher order function
6
6
function f ( val ) {
7
- val ( ) ;
8
- return function g ( ) { console . log ( "Ojha" ) ; } ;
7
+ val ( ) ;
8
+ return function g ( ) { console . log ( "Ojha" ) ; } ;
9
9
10
10
}
11
11
12
12
var gres = f ( function fx ( ) {
13
- console . log ( "Kirtti" ) ;
13
+ console . log ( "Kirtti" ) ;
14
14
15
15
} ) ;
16
16
gres ( ) ;
@@ -19,17 +19,43 @@ var arr = [1, 2, 3, 4, 5];
19
19
arr . forEach ( function ( ) { } )
20
20
21
21
22
- const radius = [ 3 , 4 , 5 , 2 ] ;
23
- function findArea ( radius ) {
24
- const res = [ ] ;
25
- for ( let i = 0 ; i < radius . length ; i ++ )
26
- {
27
- res . push ( Math . PI * radius [ i ] * radius [ i ] ) ;
28
- }
29
- return res ;
30
- }
31
22
32
- console . log ( findArea ( radius ) ) ;
23
+ // function findArea(radius) {
24
+ // const res =[];
25
+ // for(let i=0;i < radius.length; i++)
26
+ // {
27
+ // res.push(Math.PI*radius[i]*radius[i]);
28
+ // }
29
+ // return res;
30
+ // }
31
+
32
+ // console.log(findArea(radius));
33
33
//and similarly create for find diameter and circumference
34
34
//That would be not a good programming skill to repeat the code
35
35
//so the optimized and good programming skill would be
36
+ const radius = [ 3 , 4 , 5 , 2 ] ;
37
+
38
+ const area = function ( radius ) {
39
+ return Math . PI * radius * radius ;
40
+ }
41
+
42
+ const diameter = function ( radius ) {
43
+ return 2 * radius ;
44
+ }
45
+
46
+ const circumference = function ( radius ) {
47
+ return 2 * Math . PI * radius ;
48
+ }
49
+
50
+ function calculate ( radius , logic ) {
51
+ const output = [ ] ;
52
+ for ( let i = 0 ; i < radius . length ; i ++ ) {
53
+ output . push ( logic ( radius [ i ] ) ) ;
54
+ }
55
+ return output ;
56
+ }
57
+
58
+ console . log ( calculate ( radius , area ) ) ;
59
+ console . log ( calculate ( radius , circumference ) ) ;
60
+ console . log ( calculate ( radius , diameter ) ) ;
61
+
0 commit comments