-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18th.js
43 lines (33 loc) · 945 Bytes
/
18th.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
// function x() {
// console.log("Namaste");
// }
// // y here is a higher order function as it takes another function as an argument and returns it.
// function y(x) {
// x();
// }
const radius = [3, 1, 2, 4];
const area = function (radius) {
return Math.PI * radius * radius;
};
const circumference = function (radius) {
return 2 * Math.PI * radius;
};
// const calculate = function (arr, logic) {
// const output = [];
// for (let i = 0; i < arr.length; i++) {
// output.push(logic(arr[i]));
// }
// return output;
// };
console.log(radius.map(area));
// console.log(calculate(radius, circumference));
// console.log(calculate(radius, area));
// creating our own version of Map using higher order functions
Array.prototype.calculate = function (logic) {
const output = [];
for (let i = 0; i < this.length; i++) {
output.push(logic(this[i]));
}
return output;
};
console.log(radius.calculate(area));