-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.txt
More file actions
237 lines (211 loc) · 5.13 KB
/
notes.txt
File metadata and controls
237 lines (211 loc) · 5.13 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
MRF:-(Map Reduce Filter)
=> HOF (Higher Order Functions) function inside a function.
1.Map:- It iterates/transforms over each and every element in an array
and produces new array.
syntax:- arrayname.map(we need to pass function)
=>basic example without map:-
/*
without map
let arr = [12,13,14,15]
const res =[];
for(let i=0;i<arr.length;i++){
res.push(arr[i]*2)
}
console.log(res);
*/
steps:-
=> step1:- for loop.
=> step 2:- multiply the element with 2.
=> step 3:- create a new array and push into it.
=> step 4:- print the new array.
=> with map
map method:-
=>It takes a function as parameter and
apply on each and every element in an array.
=>it returns a new array
=> Maximum it will be an arrow function.(beacuse it reduce lines of code,
memory consumption and time taken)
=>It is an array method.
example:-
var res = arr.map(()=>{})
when there is only one line in function which return something.
arr.map(()=>)
=>parameter can be userdefined(ele,element)
with map
normal function
const array = [12,13,14,15]
function double(ele){
return ele*2
}
const result = array.map(double)
console.log(result);
second type function
const array = [12,13,14,15]
const result = array.map(function(ele){
return ele*2
})
console.log(result);
third type function when there is only one line in function which return something.
/*
using arrow function
const array = [12,13,14,15]
const result = array.map((ele)=>{
return ele*2
})
console.log(result);
*/
/*
using arrow function without return
const array = [12,13,14,15]
const result = array.map((ele)=>ele*2)
console.log(result);
*/
practice task:-
//!practice task:-
const fruits = ["apple","mangoe","orange"]
const result = fruits.map((ele)=>ele+"s");
console.log(result);
Filter:-
=>It is basically used to Filter😂
=> It is actually used to filter the elements inside the array.
//example without filter:-
var arr = [12,13,14,15]
o/p:- [12,14]
steps:-
=>step1:- for loop
=>step 2:- if condition(array elements divided by 2)
=>step 3:- create a new array and push the result into it
=>step 4:- printing the array.
* without filter
const array = [12,13,14,15]
const result=[];
for(let i=0;i<array.length;i++){
if(array[i]%2===0){
result.push(array[i])
}
}
console.log(result);
*/
// with filter
=> filter is based on a condition.
=> It takes a function as parameter and
apply on each and every element in an array.
=> I t returns in an new array
=> if condition is true it will push to array orelse it will be rejected
syntax:-
arrayname.filter(function)
example:-
/* with filter
normal function
const arr = [12,13,14,15]
function even(ele){
return ele%2===0
}
const result = arr.filter(even)
console.log(result);
//using annonyoums function
const arr1 = [12,13,14,15]
const res = arr1.filter(function(x){
return x%2===0
})
console.log(res);
//using arrow functions
const arr2 = [12,13,14,15]
const res1 = arr2.filter((a)=>{
return a%2===0
})
console.log(res1);
//using arrow function without return
const arr3 = [12,13,14,15]
const res2 = arr3.filter((ele)=>ele%2===0)
console.log(res2);
*/
practice task
//print the elements in an array which are greater than 3
const great = [1,2,3,4,5,6,7,8]
const result = great.filter((ele)=>ele>3)
console.log(result);
Reduce:-(confusing topic because it is not similar to the name)
=> It is generally used when we have to take all the array element and print the
single value. Example :- greatest number , sum of the array.
=> it is only for array.
=>total, summation
syntax:-
arrayname.reduce((acc,cv)=>{
},acc)
acc=> accumulator is the initial value
if initial value is not given it takes the first value.
cv=> currentvalue is the current element/value
example without reduce
/* without reduce
maximum number of an array
const arr = [1,2,3,4,5]
let max=0
for(let i=0;i<arr.length;i++){
if(arr[i]>max){
max = arr[i]
}
}
console.log(max);
using math method
const arr1=[1,2,3,4,5]
console.log(Math.max(...arr1));
sum of an array
const arr2 = [1,2,3,4,5]
let sum = 0;
for(let i=0;i<arr2.length;i++){
sum = sum+arr2[i]
}
console.log(sum);
product of an array
const arr3 = [1,2,3,4,5]
let mul = 1;
for(let i=0;i<arr3.length;i++){
mul = mul*arr3[i]
}
console.log(mul);
*/
/* With reduce
sum of an array
const array = [1,2,3,4,5];
const result = array.reduce((sum,i)=>{
return sum+i
},0)
console.log(result);
product of an array
const arr = [1,2,3,4,5]
const res = arr.reduce((mul,i)=>{
return mul*i
},1)
console.log(res);
//Maximum number of an array
const arr = [1,2,3,4,5]
const result = arr.reduce((max,i)=>{
if(i>max){
max = i
}
return max;
},0)
console.log(result);
//using math method
const arr1 = [1,2,3,4,5]
const res = arr1.reduce((max,i)=>{
return Math.max(max,i)
},0)
console.log(result);
*/
without return
const arr1 = [1,2,3,4,5]
const res = arr1.reduce((max,i)=>Math.max(max,i),0)
console.log(res);
//for each loop
=> to print the array elements
=> It is applicable only for arrays
=> It takes function as a parameter
=> It will apply on each and every element
=> It is generally used for printing.
=> It returns undefined.
example:-
var arr = [1,2,3,4,5]
var res = arr.forEach((ele)=>console.log(ele))
console.log(res)