-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.js
362 lines (281 loc) · 9.15 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
// 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 cheapestTShirt = "https://www.loom.fr/products/le-t-shirt";
console.log(cheapestTShirt);
const { Console } = require("console");
/**
* 👕
* 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
* 👕
*/
var fs = require("fs")
var vm = require('vm')
vm.runInThisContext(fs.readFileSync(__dirname + "/data.js"))
// 🎯 TODO: Number of products
// 1. Create a variable and assign it the number of products
// 2. Log the variable
var nbProducts = marketplace.length;
console.log(nbProducts);
// 🎯 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
var brandlist = [];
for (var i in marketplace){
brandlist.push(marketplace[i].brand);
}
//const brandlist = marketplace.map(product => product.brand)
function onlyUnique(value, index, self){
return self.indexOf(value) === index;
}
console.log(brandlist.filter(onlyUnique));
// 🎯 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 productsPriceSort = marketplace;
productsPriceSort.sort((a, b) => a.price - b.price);
productsPriceSort.forEach((e) => {
console.log(`${e.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 productsDateSort = marketplace;
productsDateSort.sort((product1, product2) => {
let dateProduct1 = new Date(product1.date),
dateProduct2 = new Date(product2.date);
return dateProduct2 - dateProduct1;
});
productsDateSort.forEach((e) => {
console.log(`${e.date}`);
});
// 🎯 TODO: Filter a specific price range
// 1. Filter the list of products between 50€ and 100€
// 2. Log the list
var productsFiltred50And100 = marketplace;
var productsFiltred50And100 = [];
for (var i = 0; i < marketplace.length; i++) {
if (marketplace[i].price >= 50 && marketplace[i].price <= 100) {
productsFiltred50And100.push(marketplace[i]);
}
}
productsFiltred50And100.forEach((e) => {
console.log(`${e.price}`);
});
// 🎯 TODO: Average Basket
// 1. Determine the average basket of the marketplace
// 2. Log the average
var avg = 0;
for (var i = 0; i < marketplace.length; i++) {
avg = avg + marketplace[i].price;
}
avg = avg/marketplace.length;
console.log(`there you got the average basket : ${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
// 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
const brands = {};
brandlist.filter(onlyUnique)
for (var i=0; i < brandlist.length; i++){
brands[brandlist[i]]=[];
}
for (var i=0; i < marketplace.length; i++){
brands[marketplace[i].brand].push(marketplace[i])
}
console.log(brands);
for (var e in brands){
console.log(brands[e].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
const sortedBrandsPrice=brands;
for (var e in sortedBrandsPrice){
sortedBrandsPrice[e].sort((a, b) => {
return b.price - a.price;
});
}
for (var e in sortedBrandsPrice){
sortedBrandsPrice[e].forEach((a) => {
console.log(`${a.price}`);
});
console.log("next brand");
}
// 🎯 TODO: Sort by date for each brand
// 1. For each brand, sort the products by date, from old to recent
// 2. Log the sort
const sortedBrandsDate = brands;
for (var e in sortedBrandsDate){
sortedBrandsDate[e].sort((product1, product2) => {
let dateProduct1 = new Date(product1.date),
dateProduct2 = new Date(product2.date);
return dateProduct1 - dateProduct2;
});
}
for (var e in sortedBrandsDate){
sortedBrandsDate[e].forEach((a) => {
console.log(`${a.date}`);
});
}
/**
* 💶
* 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
for (var brand in sortedBrandsPrice){
console.log(`here you got the p90 price for ${brand} : ${sortedBrandsPrice[brand][Math.floor(sortedBrandsPrice[brand].length*0.10)].price}`);
}
/**
* 🧥
* 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)
// // A reasonable price if all the products are less than 100€
// 🎯 TODO: Find a specific product
// 1. Find the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
// 2. Log the product
// 🎯 TODO: Delete a specific product
// 1. Delete the product with the uuid `b56c6d88-749a-5b4c-b571-e5b5c6483131`
// 2. Log the new list of product
// 🎯 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?
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
/**
* 🎬
* The End
* 🎬
*/
// 🎯 TODO: Save in localStorage
// 1. Save MY_FAVORITE_BRANDS in the localStorage
// 2. log the localStorage