Skip to content

Commit 9d06e33

Browse files
committed
optimized way code using higher order functions
1 parent 34fe76c commit 9d06e33

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

higherorderfunc.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
//for ex : ForEach Method alwaays takes another function inside it
55
//So forEach is a higher order function
66
function f(val) {
7-
val();
8-
return function g() { console.log("Ojha"); };
7+
val();
8+
return function g() { console.log("Ojha"); };
99

1010
}
1111

1212
var gres = f(function fx() {
13-
console.log("Kirtti");
13+
console.log("Kirtti");
1414

1515
});
1616
gres();
@@ -19,17 +19,43 @@ var arr = [1, 2, 3, 4, 5];
1919
arr.forEach(function () { })
2020

2121

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-
}
3122

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));
3333
//and similarly create for find diameter and circumference
3434
//That would be not a good programming skill to repeat the code
3535
//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

Comments
 (0)