forked from ThomasRivoal/clear-fashion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
467 lines (362 loc) · 11.9 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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
// I can find on these e-shops
// 2. Log the variable
const cheapest_tshirt = "https://www.loom.fr/products/le-t-shirt";
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
// 2. Log the variable
const number_of_products = marketplace.length;
console.log(number_of_products);
// 🎯 TODO: Brands name
// 1. Create a variable and assign it the list of brands name only
// 2. Log the variable
// 3. Log how many brands we have
// With for loops
var brands_name1 = []
for (let i = 0; i < marketplace.length; i++){
brands_name1.push(marketplace[i].brand);
}
console.log(brands_name1);
// Without for loops
var brands_name2 = marketplace.map(a => a.brand)
// with callback
marketplace.forEach(function(entry){
brands_name2.push(entry.band);
})
console.log(brands_name2);
const different_brands_name = Array.from(new Set(brands_name1));
console.log(different_brands_name);
// 🎯 TODO: Sort by price
// 1. Create a function to sort the marketplace products by price
// 2. Create a variable and assign it the list of products by price from lowest to highest
// 3. Log the variable
var marketplace_sort_by_price = marketplace.sort(function(a,b){
return a.price - b.price;
})
console.log(marketplace_sort_by_price)
// 🎯 TODO: Sort by date
// 1. Create a function to sort the marketplace objects by products date
// 2. Create a variable and assign it the list of products by date from recent to old
// 3. Log the variable
var marketplace_sort_by_date = marketplace.sort(function(a,b){
if (a.date < b.date){
return -1;
}else {
return 1;
};
})
console.log(marketplace_sort_by_date)
// 🎯 TODO: Filter a specific price range
// 1. Filter the list of products between 50€ and 100€
// 2. Log the list
const filter_price_product = marketplace.filter(obj => obj.price >= 50 && obj.price <= 100);
console.log(filter_price_product);
// 🎯 TODO: Average price
// 1. Determine the average price of the marketplace
// 2. Log the average
var price_products = marketplace.map(a => a.price);
const average = (array) => array.reduce((a,b) => a + b /array.length);
console.log(average(price_products));
/**
* 🏎
* 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
// 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
// 3. Log the number of products by brands
var brands = {};
different_brands_name.forEach(function(brand_name){
brands[brand_name] = []
for(let i = 0; i < marketplace.length; i++){
if(marketplace[i].brand === brand_name) {
brands[marketplace[i].brand].push({
'link' : marketplace[i].link,
'price' : marketplace[i].price,
'name' : marketplace[i].name,
'date' : marketplace[i].date
});
}
}
});
console.log(brands)
for(var brand in brands){
console.log('Number of product for the brand ' + brand + " : " + brands[brand].length);
}
// 🎯 TODO: Sort by price for each brand
// 1. For each brand, sort the products by price, from highest to lowest
// 2. Log the sort
for (var brand in brands){
brands[brand].sort(function(a,b){
return a.price - b.price;
});
}
console.log(brands)
// 🎯 TODO: Sort by date for each brand
// 1. For each brand, sort the products by date, from old to recent
// 2. Log the sort
for (var brand in brands){
brands[brand].sort(function(a,b){
if (a.date < b.date){
return -1;
}else {
return 1;
};
});
}
console.log(brands);
/**
* 💶
* 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
function calcQuartile(array,q){
var a = array.slice();
// Sort the array into ascending order
data = sortArr(a);
// Convert into decimal
q = q/100;
// Work out the position in the array of the percentile point
var p = ((data.length) - 1) * q;
var b = Math.floor(p);
// Work out what we rounded off (if anything)
var remainder = p - b;
// See whether that data exists directly
if (data[b+1]!==undefined){
return parseFloat(data[b]) + remainder * (parseFloat(data[b+1]) - parseFloat(data[b]));
}else{
return parseFloat(data[b]);
}
}
var p90_values = [];
var price_for_brands = [];
for (const [key, value] of Object.entries(brands)) {
//console.log(${key}: ${value});
for (let i = 0; i < value.length; i++){
price_for_brands.push(value[i].price);
}
p90_values.push(calcQuartile(price_for_brands,90));
}
console.log(p90_values)
/**
* 🧥
* 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: '2022-01-19'
},
{
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.
function new_product(product){
let new_product = false;
if ((new Date().getTime() - new Date(product.released).getTime() ) / (24*60*60*1000) < 14) {
new_product = true;
}
return new_product;
}
//i change a released date in the COTELE_PARIS object to have one new product eligible at the current date
for (let i = 0; i < COTELE_PARIS.length; i++){
if(new_product(COTELE_PARIS[i]) == true){
console.log("This is a new product : " + COTELE_PARIS[i].link);
}
}
// 🎯 TODO: Reasonable price
// // 1. Log if coteleparis is a reasonable price shop (true or false)
// // A reasonable price if all the products are less than 100€
let number_product = COTELE_PARIS.length;
let reasonable_price = 0;
for (let i = 0; i < COTELE_PARIS.length; i++){
if (COTELE_PARIS[i].price < 100){
reasonable_price++;
}
}
if(number_product == reasonable_price){
console.log("COTELE PARIS is a reasonable shop!");
}
else console.log("COTELE PARIS is not a reasonable shop!");
//other manner with true/false verification
function reasonable_price_shop(products){
let reasonable = 0;
for (let i = 0; i < COTELE_PARIS.length; i++){
if(COTELE_PARIS[i].price < 100){
reasonable++;
}
}
return reasonable == COTELE_PARIS.length;
}
console.log(reasonable_price_shop(COTELE_PARIS));
// 🎯 TODO: Find a specific product
// 1. Find the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
// 2. Log the product
function find_product_with_uuid(products, uuid){
let product = {}
for (let i = 0; i < products.length; i++){
if(products[i].uuid === uuid){
product = products[i];
}
}
return product
}
console.log(find_product_with_uuid(COTELE_PARIS,'b56c6d88-749a-5b4c-b571-e5b5c6483131'));
// 🎯 TODO: Delete a specific product
// 1. Delete the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
// 2. Log the new list of product
// console.log("LOG BEFORE DELETION : ");
// console.log(COTELE_PARIS);
function delete_product_by_uuid(uuid, products){
for (let i = 0; i < products.length; i++){
if(products[i].uuid === uuid){
console.log(products[i]);
products.splice(i,1); //we want to delete only one element at the specified index
break;
}
}
return products
}
console.log("LOG AFTER DELETION : ");
console.log(delete_product_by_uuid('b56c6d88-749a-5b4c-b571-e5b5c6483131', 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
// 2. What do you notice?
console.log('blueJacket :>> ', blueJacket);
console.log('jacket :>> ', jacket);
// we can notice that the two variables are exactly the same even after adding a new property to jacket. The both have this property now
// it seems that when a 2 variables are copy of each other, if a modification affects one variable, the other will be affect too
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 = Object.assign({}, blueJacket);
jacket.favorite = true;
console.log('blueJacket :>> ', blueJacket);
console.log('jacket :>> ', jacket);
/**
* 🎬
* The End
* 🎬
*/
// 🎯 TODO: Save in localStorage
// 1. Save MY_FAVORITE_BRANDS in the localStorage
// 2. log the localStorage
localStorage.setItem('MY_FAVORITE_BRANDS', MY_FAVORITE_BRANDS);
console.log(MY_FAVORITE_BRANDS);