-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
313 lines (191 loc) · 6.53 KB
/
app.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// simple declaration
console.log("array=[]");
const numbers=[1,2,3,4];
console.log(numbers);
//new Array()
console.log("array= new Array()");
const moreNumbers= new Array(1,2,3,4,5);
console.log(moreNumbers);
const names=new Array("azim","rian","aamir");
console.log(names);
// const moreNumbers=new Array(5) single parameter not allowed as it makes itself the length
//Array.of();
console.log("array=Array.of()");
const num=Array.of(1,2,3);
console.log("array.of()",num);
const n=Array.of(1)
console.log(n);
//Array.from()
console.log("Array.from() for iterables");
const arraylist=Array.from("Azim");
console.log(arraylist);
const listItems=document.querySelectorAll('li');
console.log(listItems);
const arrayItems= Array.from(listItems);
console.log(arrayItems);
const hobbies=['cooking','playing','reading'];
console.log(hobbies);
const mix=[30,'max',{moreDetails:[]}];
console.log(mix[1]);
const twoD=[[1,2,3,4],[5,6,7,8]];
for(const data of twoD){
for(const dataPoints of data){
console.log(dataPoints);
}
}
const azimHobbies=['cooking','playing'];
azimHobbies.push('reading');
console.log(azimHobbies);
azimHobbies.unshift('coding');
console.log(azimHobbies);
azimHobbies.pop();
console.log(azimHobbies);
azimHobbies.shift();
console.log(azimHobbies);
azimHobbies[5]='coding';
console.log(azimHobbies);
// deleting from a particular place in array
azimHobbies.splice(0,0,'good coding');
console.log(azimHobbies);
// deleting from last
azimHobbies.splice(-1,1);
console.log(azimHobbies);
// upper limit is excluded in slice
const testResults=[10,-1,2,3,4,-10,5,6,7];
const resultCopy=testResults.slice(2);
console.log(testResults,resultCopy);
const resultCopy1=testResults.slice(-3,-1);
console.log(resultCopy1);
// creating a copy of array elements using concat
const storedResult=testResults.concat([1,1,1,1,1,1]);
console.log(storedResult);
// for getting the index of array elements is indexOf(),lastIndexOf()
console.log(testResults.indexOf(2));
console.log(testResults.lastIndexOf(-10));
// but for refrence values(objects) the index does not work which gives -1 (error)
const Name=[{name:'max'},{name:'azim'}];
console.log(Name.indexOf({name:'azim'}));
// find()
const personData=[{name:'Azim'},{name:'Max'}];
const azim=personData.find((person,indx,persons)=>
{
return person.name==="Azim";
});
console.log(azim);
// findIndex()
const maxInd=personData.findIndex((person,indx,persons)=>
{
return person.name==='Max';
});
console.log(maxInd);
// includes() to find the element present in the array
const test=[1,2,3,4,5,-5];
console.log(test.includes(-5));
console.log(test.includes(-10));
//foreach() method for each element in an array
const prices=[1,2,3,4,5,6]
const tax=0.19;
const taxAdjustedPrice=[];
prices.forEach((price,idx,prices)=>{
const priceObj={indx:idx,taxAdjustedprice:price*(1+tax)};
taxAdjustedPrice.push(priceObj);
});
console.log(taxAdjustedPrice);
// map() function to access the elements in an array for refrence as a object
const price1=[1,2,3,4,5];
const tax1=1.5;
const newAdjust=price1.map((price,idx,price1)=>{
const priceObj={index:idx,taxprice:(1+tax)*price};
return priceObj;
});
console.log(newAdjust,price1);
// sorting using sort()
const arr=[10,9,8,7,6,5];
// const sortedPrice= arr.sort();
const sortedPrice=arr.sort((a,b)=>{
if(a>b){
return 1;
}
else if(a===b){
return 0;
}
else{
return -1;
}
});
console.log(sortedPrice);
//sorting in reverse order
const arr1=[1,2,3,4,5];
const reversePrice=arr.sort((a,b)=>{
if(a>b){
return -1;
}
else if(a===b){
return 0;
}
else{
return 1;
}
});
console.log(reversePrice);
// using filter()
const priceArr=[6,7,8,5,10];
const filterArr=priceArr.filter((price,idx,priceArr)=>{
return price>6;
});
console.log(priceArr);
console.log(filterArr);
// using => functions for shorter code and clean code
const newArr=[1,2,3,4,5];
const filterNewArr=newArr.filter(p => p>2);
console.log(filterNewArr);
// using reduce() function for reducing code with 2 parameters
const sum=newArr.reduce((preValue,curValue)=>preValue+curValue,0);
console.log(sum);
// CHANING METHODS IN JAVASCRIPT(USING MAPING REDUCING AND FILTERING CONCEPT)
const originalArray=[{price:10.99},{price:5.99},{price:29.99}];
const transformedArray=originalArray.map(obj=>obj.price);
const sum1=transformedArray.reduce((sumVal,curVal)=>{
sumVal+curVal
},0);
console.log(sum1);
// reduce()
const origArr=[{price:10.99},{price:5.99},{price:29.99}];
const sumTwo=origArr.reduce((sumVal,curVal)=>sumVal+curVal,0);
console.log(sumTwo);
// CHANING EXAMPLE
const origArray=[{price:10.99},{price:5.99},{price:29.99}];
const sumThree=origArray.map(obj=>obj.price).reduce((sumVal,curVal)=>sumVal+curVal,0);
console.log(sumThree);
// .spilt() method used convert String Data to Array
const data="newYork;2021;1030";
const transArray=data.split(";");
transArray[1]=+transArray[1];
console.log(transArray);
// .join() method used to convert Array elements to string
const nameFragments=["Mohammed","Azim"];
const nameString=nameFragments.join(" ");
console.log(nameString);
// use of SPREAD OPERATOR(...) for arrays
const nameFrag=["Mohammed","Azim"];
const copiedFrag=[...nameFrag]; //pulling each individual element and copying it out variable using ... operator
nameFrag.unshift("Mr");
console.log(nameFrag,copiedFrag);
// ... spread oprator to pass array elements in an in built methods (which cannot be passed as array)
const priceOne=[1,2,3,4,5,-1,-2,-3];
console.log(Math.min(...priceOne));
// Spread opreator(...) for objects
const personObj=[{name:'Azim',age:'18'},{name:"Ummar",age:"21"}];
const copiedPerson=[...personObj];
// personObj[0].age="21"; // ISSUE the copied object copies the old refrence address
console.log(personObj,copiedPerson);
// ISSUE SOLVED using map() functions
// new object whit new addressing
const copiedPersons=[personObj.map(person=>({name:person.name,age:person.age}))];
personObj[0].age="21";
console.log("issue",personObj);
console.log("issue solved",copiedPersons);
// Destructing Arrays
const nameData=["Mohammed","Azim","Mr",21];
const [firstName,lastName,...otherInfo]=nameData;
console.log(firstName,lastName,otherInfo);