forked from digital-dynamite/digital_interview_questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.js
67 lines (53 loc) · 2.83 KB
/
6.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
/*
QUESTION 6
You are working on the product categories page and are responsible to display all the products for the given category (i.e. product tiles).
Each product tile can contain multiple images for the given product (will be displayed using a carousel).
The image src needs to scale according to screen size in order to better optimize page loading times.
This can be done by using the image `srcset` attribute whose value should be a string with the following format: `${imageUrl} #{widthInPixels}w`
Multiple widths can be specified by comma separating the values.
E.g.
srcset="https://myCdn.com/productId_1_320x480.png 320w, https://myCdn.com/productId_1_640x960.png 640w, https://myCdn.com/productId_1_768x1152.png 1080w"
Your task is to add this `srcset` to each image of every product tile.
You will be given the list of raw product Objects which will be used to generate the page:
- Each product Object will have an `imageUrls` attribute which will be a list of image URLs
- Each URL will contain within it the corresponding product Id, the image index, and the image size:
`https://myCdn.com/${productId}_${index}_${width}x${height}.png`
- Some products might be missing some image sizes (in which case just omit that case from the `srcset`)
- All images have a 3/2 image aspect ratio (height/width = 1.5)
- Some browser do not yet suport the `srcset` attribute so you need to also provide a `src` attribute that defaults to using the *LARGEST* image resolution
This allows the browser to fallback to using the `src` if `srcset` is not yet supported
You need to prodive a way to map each `srcset` and `src` to the given product and it's image index.
e.g.
const rawProducts = [
{
id: 'product_id_1',
imagesUrls: [
https://myCdn.com/productId-1_1_320x480.png,
https://myCdn.com/productId-1_1_768x1152.png,
https://myCdn.com/productId-1_2_320x480.png,
https://myCdn.com/productId-1_2_640x960.png,
https://myCdn.com/productId-1_2_768x1152.png,
https://myCdn.com/productId-1_3_320x480.png,
]
}
]
should become
const formattedProducts = [
{
id: 'product_id_1',
imageUrls: [...],
srcset: {
1: 'https://myCdn.com/productId-1_1_320x480.png 320w, https://myCdn.com/productId-1_1_768x1152.png 1080w',
2: 'https://myCdn.com/productId-1_2_320x480.png 320w, https://myCdn.com/productId-1_2_640x960.png 640w, https://myCdn.com/productId-1_2_768x1152.png 1080w',
3: 'https://myCdn.com/productId-1_3_320x480.png 320w',
},
src: {
1: 'https://myCdn.com/productId-1_1_768x1152.png',
2: ' https://myCdn.com/productId-1_2_768x1152.png',
3: 'https://myCdn.com/productId-1_3_320x480.png'
}
}
]
*/
//DO NOT modify this
const input = require('./6-input')