Skip to content

Commit 480bd91

Browse files
higher order functions
1 parent b0da61e commit 480bd91

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

HigherOrderFunction/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function multiplier(factor) {
2424
}
2525

2626
const double = multiplier(2);
27+
console.log(double); // Output: [Function: anonymous]
28+
// Using the returned function
2729
console.log(double(5)); // Output: 10
2830

2931
// 2. Why use Higher Order Functions?

JS-Interview-Questions/HigherOrderFunction.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,41 @@ y(); // Hi
2525

2626
## Examples
2727

28-
### 1. Passing a function as an argument
28+
### 1. Passing a Function as an Argument
29+
2930
```js
3031
function greet(name) {
3132
return `Hello, ${name}!`;
3233
}
34+
3335
function processUserInput(callback) {
3436
const name = 'Aditya';
3537
return callback(name);
3638
}
39+
3740
console.log(processUserInput(greet)); // Output: Hello, Aditya!
3841
```
42+
**Explanation:**
43+
Here, `processUserInput` is a higher order function because it takes another function (`callback`) as an argument. You pass the `greet` function to it, so `processUserInput` calls `greet` with the name "Aditya". This is a common pattern for handling user input, events, or callbacks.
44+
45+
---
46+
47+
### 2. Returning a Function (Closure)
3948

40-
### 2. Returning a function
4149
```js
4250
function multiplier(factor) {
4351
return function(number) {
4452
return number * factor;
4553
};
4654
}
55+
4756
const double = multiplier(2);
4857
console.log(double(5)); // Output: 10
4958
```
59+
**Explanation:**
60+
`multiplier` is a higher order function because it returns a new function. When you call `multiplier(2)`, you get a function that multiplies any number by 2. This is called a closure, because the returned function "remembers" the value of `factor`. So, `double(5)` gives you `10`.
61+
62+
---
5063

5164
### 3. Built-in Higher Order Functions
5265
There are various built in HOFs, and some of the most common ones are map(), filter() and reduce().
@@ -75,6 +88,8 @@ function repeat(n, action) {
7588
// Here, console.log is passed as the action, so it logs 0, 1, 2
7689
repeat(3, console.log); // Logs 0, 1, 2
7790
```
91+
**Explanation:**
92+
The `repeat` function is a higher order function because it takes another function (`action`) as an argument. You can pass any function to `repeat`, and it will call that function for each number from 0 to n-1. In this example, we pass `console.log`, so it prints 0, 1, and 2.
7893

7994
### 5. Returning Functions (Closures)
8095
```js
@@ -92,6 +107,8 @@ console.log(counter()); // 1
92107
console.log(counter()); // 2
93108
// Each call to counter() increases and returns the count
94109
```
110+
**Explanation:**
111+
`makeCounter` is a higher order function because it returns a new function. The returned function "remembers" the value of `count` even after `makeCounter` has finished running. This is called a closure. Each time you call `counter()`, it increases and returns the count, keeping track of how many times it was called.
95112

96113
### 6. Function Composition
97114
```js
@@ -107,6 +124,8 @@ const add1ThenTimes2 = compose(times2, add1);
107124
console.log(add1ThenTimes2(5)); // (5 + 1) * 2 = 12
108125
// add1ThenTimes2 first adds 1 to 5, then multiplies the result by 2
109126
```
127+
**Explanation:**
128+
The `compose` function is a higher order function because it takes two functions as arguments and returns a new function. The returned function applies `g` to its input, then applies `f` to the result. In the example, `add1ThenTimes2(5)` first adds 1 to 5 (getting 6), then multiplies by 2 (getting 12).
110129

111130
## Interview Approach Example: Calculating Area and Circumference
112131

@@ -131,6 +150,8 @@ const calculateCircumference = function (radius) {
131150
};
132151
console.log(calculateCircumference(radius));
133152
```
153+
**Explanation:**
154+
This approach works, but it repeats similar logic for area and circumference. If you need to add more calculations, you would have to write more similar functions, which is not efficient and violates the DRY (Don't Repeat Yourself) principle.
134155

135156
### Better Approach Using Higher Order Function
136157
```js
@@ -165,6 +186,8 @@ const circumferences = radiusArr.map(circumference);
165186
console.log(circumferences);
166187

167188
```
189+
**Explanation:**
190+
Here, we use a higher order function `calculate` that takes an array and a function (`operation`) as arguments. This makes the code reusable and clean. You can pass any calculation logic (like area or circumference) to `calculate`, and it will apply that logic to each element in the array. This is the power of higher order functions and functional programming.
168191

169192
### Polyfill of map (Custom Implementation)
170193
```js
@@ -180,6 +203,8 @@ Array.prototype.calculate = function(operation) {
180203
}
181204
console.log(radiusArr.calculate(area));
182205
```
206+
**Explanation:**
207+
This is a custom implementation (polyfill) of the built-in `map` function. By adding `calculate` to `Array.prototype`, you can use it on any array just like `map`. It takes a function as an argument and applies it to every element in the array, returning a new array with the results.
183208

184209
## Summary
185210
Higher order functions are a key concept in JavaScript, enabling powerful patterns for abstraction, reusability, and functional programming.

0 commit comments

Comments
 (0)