Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit91fd4d9

Browse files
Added Higher order functions and Map, Filter and Reduce
1 parent17eb416 commit91fd4d9

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8">
5+
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
6+
<metaname="viewport"content="width=device-width, initial-scale=1.0">
7+
<title>Higher Order Functions</title>
8+
<scriptsrc="script.js"></script>
9+
</head>
10+
<body>
11+
<h1>Higher Order Functions</h1>
12+
<h3>A function which takes another function as an arguement or
13+
returns a function from it
14+
</h3>
15+
16+
</body>
17+
</html>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
console.log("Map Filter and Reduce are Higher order functions in JS.");
2+
console.log("Map, filter and reduce all do't change the original array.")
3+
constarr=[2,4,6,9];
4+
5+
// console.log(arr.map(x=> x*2)) // doubling the array values
6+
// console.log(arr.map(function binary(x){ // converting array values into binary
7+
// return x.toString(2)
8+
// }))
9+
// console.log(arr) // map method doesn't change the original array
10+
11+
12+
// Filter function is used to filter values from an array and return the new filtered array
13+
14+
// let a = [1,2,3,4,5,6,7,8];
15+
// console.log(a.filter(x=>x%2==0)); // filter the even values from array
16+
// console.log(a) // filter method doesn't change the original array
17+
18+
19+
// Reduce function is used to iterate over an array and find takes out single value from it
20+
letarray=[10,2,13,44,25,9];
21+
22+
// find sum
23+
functionfindSum(array){
24+
letsum=0;
25+
for(leti=0;i<array.length;i++){
26+
sum=sum+array[i];
27+
}
28+
returnsum;
29+
}
30+
console.log(findSum(array))
31+
32+
// Using Reduce function
33+
console.log(
34+
array.reduce(function(accumulator,currentValue){
35+
accumulator=accumulator+currentValue;
36+
returnaccumulator
37+
},0))
38+
39+
console.log(
40+
array.reduce((accumulator,currentValue)=>
41+
accumulator=accumulator+currentValue,0
42+
))
43+
44+
// Example 2 :- Find maximum element from an array
45+
46+
functionfindMax(array){
47+
letmax=Number.MIN_VALUE;
48+
for(leti=0;i<array.length;i++){
49+
if(max<array[i])max=array[i];
50+
}
51+
returnmax;
52+
}
53+
54+
console.log(findMax(array));
55+
56+
// using reduce function
57+
console.log(
58+
array.reduce(function(maximumValue,currentValue){
59+
if(maximumValue<currentValue)maximumValue=currentValue;
60+
returnmaximumValue
61+
},intialValue=Number.MIN_VALUE))
62+
63+
64+
// Example 3 :- Reduce
65+
constusers=[
66+
{"fName":"Rajan","lName":"Walia","age":22},
67+
{"fName":"Mukul","lName":"Walia","age":23},
68+
{"fName":"Aman","lName":"Sharma","age":19},
69+
{"fName":"Sahu","lName":"Tech","age":26},
70+
{"fName":"Pranjal","lName":"Aggarwal","age":30},
71+
{"fName":"Jatin","lName":"Don","age":22}
72+
]
73+
// find the all users with full name
74+
console.log(
75+
users.map(user=>{
76+
returnuser.fName+" "+user.lName
77+
})
78+
)
79+
80+
81+
// Find the count of each age of users
82+
console.log(users.reduce(function(accumulator,currentUser){
83+
if(accumulator[currentUser.age]){
84+
accumulator[currentUser.age]++;
85+
}else{
86+
accumulator[currentUser.age]=1;
87+
}
88+
returnaccumulator;
89+
},{}))
90+
91+
// Example 4. find the users name with age less than 25
92+
// chaining of map filter reduce
93+
console.log(
94+
users.filter(function(user){
95+
if(user.age<25)returnuser
96+
})
97+
.map(user=>user.fName)
98+
)
99+
100+
101+
// Using reduce
102+
console.log(users.reduce(function(accumulator,currentUser){
103+
if(currentUser.age<25)
104+
accumulator.push(currentUser.fName);
105+
returnaccumulator;
106+
},[]))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
console.log("Higher Order Functions - A function which takes another function as an arguement or returns a function from it ");
2+
functionx(){
3+
console.log("HEllo");
4+
}
5+
6+
functiony(callback){// Here y is HOF and x is a callback
7+
callback();
8+
}
9+
10+
// y(x);
11+
12+
constradius=[3,4,5,2];
13+
constarea=(r)=>{
14+
returnMath.PI*r*r;
15+
}
16+
constcircumference=(r)=>{
17+
return2*Math.PI*r;
18+
}
19+
constdiameter=(r)=>{
20+
return2*r;
21+
}
22+
23+
constcalculate=function(radius,logic){
24+
constoutput=[];
25+
for(leti=0;i<radius.length;i++){
26+
output.push(logic(radius[i]));
27+
}
28+
returnoutput;
29+
}
30+
// console.log(radius)
31+
// console.log(calculate(radius,area));
32+
// console.log(calculate(radius,circumference));
33+
// console.log(calculate(radius,diameter));
34+
// console.log(radius.map(area));
35+
36+
// functional programmming says that think everything in functions
37+
// and reuse those functions
38+
39+
// this is pollyfill of map
40+
Array.prototype.calculate=function(logic){
41+
constoutput=[];
42+
for(leti=0;i<this.length;i++){
43+
output.push(logic(this[i]));
44+
}
45+
returnoutput;
46+
}
47+
// Map is a HOF in JS
48+
console.log(radius.map(area));
49+
console.log(radius.calculate(area));

‎02. 01 Array Methods/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8">
5+
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
6+
<metaname="viewport"content="width=device-width, initial-scale=1.0">
7+
<scriptsrc="script.js"></script>
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
12+
</body>
13+
</html>

‎02. 01 Array Methods/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp