-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathindex.js
336 lines (285 loc) · 9.56 KB
/
index.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Invoking strict mode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#invoking_strict_mode
'use strict';
console.log('🚀 This is it.');
const MY_FAVORITE_BRANDS = [{
'name': 'Hopaal',
'url': 'https://hopaal.com/'
}, {
'name': 'Loom',
'url': 'https://www.loom.fr'
}, {
'name': 'ADRESSE',
'url': 'https://adresse.paris/'
}];
console.table(MY_FAVORITE_BRANDS);
console.log(MY_FAVORITE_BRANDS[0]);
/**
* 🌱
* Let's go with a very very simple first todo
* Keep pushing
* 🌱
*/
// 🎯 TODO: The cheapest t-shirt
// 0. I have 3 favorite brands stored in MY_FAVORITE_BRANDS variable
// 1. Create a new variable and assign it the link of the cheapest t-shirt
const cheapest_tshirt = 'https://www.adresse.paris/t-shirts-et-polos/4238-t-shirt-ranelagh-1300000262026.html'
// I can find on these e-shops
// 2. Log the variable
console.log(cheapest_tshirt)
/**
* 👕
* Easy 😁?
* Now we manipulate the variable `marketplace`
* `marketplace` is a list of products from several brands e-shops
* The variable is loaded by the file data.js
* 👕
*/
// 🎯 TODO: Number of products
// 1. Create a variable and assign it the number of products
var longueur = marketplace.length
// 2. Log the variable
console.log(longueur)
// 🎯 TODO: Brands name
// 1. Create a variable and assign it the list of brands name only
let brand_names=[]
for(var i=0; i<marketplace.length;i++){
if(!(brand_names.includes(marketplace[i].brand))){
brand_names.push(marketplace[i].brand)
}
}
// 2. Log the variable
console.log(brand_names)
// 3. Log how many brands we have
console.log(brand_names.length)
// 🎯 TODO: Sort by price
// 1. Create a function to sort the marketplace products by price
function sortbyprice(){
var marketplacebyPrice= marketplace.sort((a,b) => (a.price>b.price)?1:-1);
return marketplacebyPrice;
}
// 2. Create a variable and assign it the list of products by price from lowest to highest
let sorted_products_price = sortbyprice()
// 3. Log the variable
console.log(sorted_products_price)
// 🎯 TODO: Sort by date
// 1. Create a function to sort the marketplace objects by products date
function sort_by_date_asc(marketplace){
return marketplace.sort((a, b) => (a.date<b.date) ? 1 : -1)
}
// 2. Create a variable and assign it the list of products by date from recent to old
var sorted_product_by_date = sort_by_date_asc(marketplace)
// 3. Log the variable
console.log(sorted_product_by_date)
// 🎯 TODO: Filter a specific price range
// 1. Filter the list of products between 50€ and 100€
var list_50_100=marketplace.filter(function(products){
return (products.price>50 && products.price<100)
});
// 2. Log the list
console.log(list_50_100)
// 🎯 TODO: Average price
// 1. Determine the average price of the marketplace
function average_price(){
var sum=0
for(var i=0;i<marketplace.length;i++){
sum+=marketplace[i].price;
}
return sum/marketplace.length
}
// 2. Log the average
let avg=average_price()
console.log(avg)
/**
* 🏎
* We are almost done with the `marketplace` variable
* Keep pushing
* 🏎
*/
// 🎯 TODO: Products by brands
// 1. Create an object called `brands` to manipulate products by brand name
function groupArrayOfObjects(list, key) {
return list.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var brands=groupArrayOfObjects(marketplace,"brand");
// The key is the brand name
// The value is the array of products
//
// Example:
// const brands = {
// 'brand-name-1': [{...}, {...}, ..., {...}],
// 'brand-name-2': [{...}, {...}, ..., {...}],
// ....
// 'brand-name-n': [{...}, {...}, ..., {...}],
// };
//
// 2. Log the variable
console.log(brands)
// 3. Log the number of products by brands
for(var obj in brands){
console.log(obj)
console.log(brands[obj].length)
}
// 🎯 TODO: Sort by price for each brand
// 1. For each brand, sort the products by price, from highest to lowest
function sort_by_price_desc(marketplace){
return marketplace.sort((a, b) => (a.price > b.price) ? -1 : 1)
}
var brands_sorted_by_price_desc = brands
for(var obj in brands_sorted_by_price_desc){
brands_sorted_by_price_desc[obj] = sort_by_price_desc(brands_sorted_by_price_desc[obj])
}
// 2. Log the sort
console.log(brands_sorted_by_price_desc)
// 🎯 TODO: Sort by date for each brand
// 1. For each brand, sort the products by date, from old to recent
var brands_sorted_by_date_asc = brands
for(var obj in brands_sorted_by_date_asc){
brands_sorted_by_date_asc[obj] = sort_by_date_asc(brands_sorted_by_date_asc[obj])
}
// 2. Log the sort
console.log(brands_sorted_by_date_asc)
/**
* 💶
* Let's talk about money now
* Do some Maths
* 💶
*/
// 🎯 TODO: Compute the p90 price value
// 1. Compute the p90 price value of each brand
// The p90 value (90th percentile) is the lower value expected to be exceeded in 90% of the products
var brands_sorted_by_price_asc = brands
for(var obj in brands_sorted_by_price_asc){
brands_sorted_by_price_asc[obj] = sort_by_price_asc(brands_sorted_by_price_asc[obj])
}
for(var obj in brands_sorted_by_price_asc){
console.log(obj)
console.log(brands_sorted_by_price_asc[obj][Math.round(brands_sorted_by_price_asc[obj].length*0.9)-1])
}
/**
* 🧥
* Cool for your effort.
* It's almost done
* Now we manipulate the variable `COTELE_PARIS`
* `COTELE_PARIS` is a list of products from https://coteleparis.com/collections/tous-les-produits-cotele
* 🧥
*/
const COTELE_PARIS = [
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-baseball-cap-gris',
price: 45,
name: 'BASEBALL CAP - TAUPE',
uuid: 'af07d5a4-778d-56ad-b3f5-7001bf7f2b7d',
released: '2021-01-11'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-chemise-milleraie-navy',
price: 85,
name: 'CHEMISE MILLERAIE MIXTE - NAVY',
uuid: 'd62e3055-1eb2-5c09-b865-9d0438bcf075',
released: '2020-12-21'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-veste-fuchsia',
price: 110,
name: 'VESTE - FUCHSIA',
uuid: 'da3858a2-95e3-53da-b92c-7f3d535a753d',
released: '2020-11-17'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-baseball-cap-camel',
price: 45,
name: 'BASEBALL CAP - CAMEL',
uuid: 'b56c6d88-749a-5b4c-b571-e5b5c6483131',
released: '2020-10-19'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-chemise-milleraie-beige',
price: 85,
name: 'CHEMISE MILLERAIE MIXTE - BEIGE',
uuid: 'f64727eb-215e-5229-b3f9-063b5354700d',
released: '2021-01-11'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-veste-rouge-vermeil',
price: 110,
name: 'VESTE - ROUGE VERMEIL',
uuid: '4370637a-9e34-5d0f-9631-04d54a838a6e',
released: '2020-12-21'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/la-chemise-milleraie-bordeaux',
price: 85,
name: 'CHEMISE MILLERAIE MIXTE - BORDEAUX',
uuid: '93d80d82-3fc3-55dd-a7ef-09a32053e36c',
released: '2020-12-21'
},
{
link: 'https://coteleparis.com//collections/tous-les-produits-cotele/products/le-bob-dylan-gris',
price: 45,
name: 'BOB DYLAN - TAUPE',
uuid: 'f48810f1-a822-5ee3-b41a-be15e9a97e3f',
released: '2020-12-21'
}
]
// 🎯 TODO: New released products
// // 1. Log if we have new products only (true or false)
// // A new product is a product `released` less than 2 weeks.
// 🎯 TODO: Reasonable price
// // 1. Log if coteleparis is a reasonable price shop (true or false)
var is_reasonable = true
COTELE_PARIS.forEach(obj => (obj.price>100)?is_reasonable=false:null)
// // A reasonable price if all the products are less than 100€
console.log(is_reasonable)
// 🎯 TODO: Find a specific product
// 1. Find the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
var tmp = null
COTELE_PARIS.forEach(obj => (obj.uuid=="b56c6d88-749a-5b4c-b571-e5b5c6483131")?tmp=obj:null)
// 2. Log the product
console.log(tmp)
// 🎯 TODO: Delete a specific product
// 1. Delete the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
for( var i = 0; i < COTELE_PARIS.length; i++){
if ( COTELE_PARIS[i].uuid == "b56c6d88-749a-5b4c-b571-e5b5c6483131") {
COTELE_PARIS.splice(i, 1);
i--;
}
}
// 2. Log the new list of product
console.log(COTELE_PARIS)
// 🎯 TODO: Save the favorite product
let blueJacket = {
'link': 'https://coteleparis.com/collections/tous-les-produits-cotele/products/la-veste-bleu-roi',
'price': 110,
'uuid': 'b4b05398-fee0-4b31-90fe-a794d2ccfaaa'
};
// we make a copy of blueJacket to jacket
// and set a new property `favorite` to true
let jacket = blueJacket;
jacket.favorite = true;
// 1. Log `blueJacket` and `jacket` variables
console.log(blueJacket)
console.log(jacket)
// 2. What do you notice?
// Copy didn't work because both are linked by the same variable address
blueJacket = {
'link': 'https://coteleparis.com/collections/tous-les-produits-cotele/products/la-veste-bleu-roi',
'price': 110,
'uuid': 'b4b05398-fee0-4b31-90fe-a794d2ccfaaa'
};
// 3. Update `jacket` property with `favorite` to true WITHOUT changing blueJacket properties
jacket = {...blueJacket}
jacket.favorite = true
console.log(blueJacket)
console.log(jacket)
// 3. Update `jacket` property with `favorite` to true WITHOUT changing blueJacket properties
/**
* 🎬
* The End
* 🎬
*/
// 🎯 TODO: Save in localStorage
// 1. Save MY_FAVORITE_BRANDS in the localStorage
// 2. log the localStorage