-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (125 loc) · 4.03 KB
/
Copy pathscript.js
File metadata and controls
197 lines (125 loc) · 4.03 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
// . ## array cardio day 1
// . Some data we can work with
const legends =[
{ first : 'Albert', last : 'Elinstein', year : 1879, passed : 1955},
{ first : 'Ali', last : 'Ahmed', year : 1643, passed : 1727},
];
const people = [
'Beck, glenn', 'haris, rouf','babar, azam', ' mani , ahad', 'hamza, alle', 'arslan, raza'
];
// Array.prototype.filter()
// 1. Filter the list of legends for those who were born in the 1500's
const fifteen = legends.filter(function(legend){
if(legend.year >= 1500 && legend.year > 1600){
return true ;
}
});
console.table(fifteen);
// filter with arrow function
// const fifteen = legends.filter(legend => (legend.year >= 1500 && legend.year > 16));
// console.table(fifteen);
// FIlter pratice
const numbers = [5, 12, 18, 3, 25, 7];
const teen = numbers.filter(num=> num > 10);
console.table(teen);
const ages = [12, 19, 25, 14, 30, 16];
const year = ages.filter(age=> age >=18);
console.log(year);
const prices = [100, 450, 800, 1200, 300];
const price = prices.filter(pric=>pric>500);
console.log(price);
// Array or objects with filter
const products = [
{ name: 'Mobile', price: 30000 },
{ name: 'Charger', price: 800 },
{ name: 'Laptop', price: 120000 },
{ name: 'Cable', price: 500 }
];
const pro = products.filter(product => product.price >= 10000);
console.table(pro);
// /Array.prototype.map()
// 2. give us an array of the legends first and last
// const fullNames = legends.map(legend => legend.first + legend.last);
// console.log(fullNames);
// with space
// const fullNames = legends.map(legend => `${legend.first} ${legend.last}` );
// console.table(fullNames);
// + operator for manually " " space
// const fullNames = legends.map(legend => legend.first + " " + legend.last);
// console.log(fullNames);
// / more pratice on map()
const digits = [1, 2, 3, 4, 5];
const digit = digits.map(dig => dig * 10);
console.table(digit);
const names = ['Ali', 'Haris', 'Usman'];
const name = names.map(nam=>nam+" khan");
console.table(name);
const users = [
{ name: 'Ali', age: 17 },
{ name: 'Haris', age: 22 },
{ name: 'Usman', age: 19 }
];
const user = users.map(use=> `${use.name}`);
console.table(user);
Array.prototype.sort()
// 3. Sort the legends by birthday, oldest to youngest
const ordered = legends.sort(function (a , b) {
if(a.year > b.year){
return 1 ;
} else {
return -1;
}
});
console.table(ordered);
// arrow function with clean and short code on sort()
// const ordered = legends.sort((a,b)=> a.year - b.year);
// console.table(ordered);
// More pratice on sort ()
// const numbers = [45, 3, 22, 10, 99, 1];
// const num = numbers.sort((a,b) => a - b);
// console.table(num);
// accending order
// const numbers = [45, 3, 22, 10, 99, 1];
// const num = numbers.sort((a,b)=> b - a);
// console.table(num);
// descending order
const players = [
{ name: 'Ali', score: 80 },
{ name: 'Sara', score: 95 },
{ name: 'Ahmed', score: 60 },
{ name: 'Zara', score: 90 }
];
const play = players.sort((a,b)=> b.score - a.score);
console.table(play);
// sorting alphabetical order A->Z
// const players = [
// { name: 'Ali', score: 80 },
// { name: 'Sara', score: 95 },
// { name: 'Ahmed', score: 60 },
// { name: 'Zara', score: 90 }
// ];
const paly = players.sort((a,b)=> a.name.localeCompare (b.name));
console.table(paly);
// / Array.prototype.reduce()
// 4 how many years did all the legends live
const totalYears = legends.reduce((a , b)=>{
return a + (b.passed - b.year);
},0)
console.log(totalYears)
// / More practice on reduce() On shopping cart
const cart = [
{ item: 'Milk', price: 200 },
{ item: 'Bread', price: 150 },
{ item: 'Eggs', price: 300 },
];
const totalBill = cart.reduce((a , b)=> a + b.price,0);
console.table(totalBill);
// 5 Sort the legends by years lived
const oldest = legends.sort((a , b) =>{
const lastGuy = a.passed - a.year;
const nextGuy = b.passed - b.year;
return lastGuy > nextGuy ? -1 : 1 ;
});
console.table(oldest);
const alpha = people.sort((a,b)=> a.localeCompare(b));
console.table(alpha);