This repository was archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (59 loc) · 2.15 KB
/
app.js
File metadata and controls
71 lines (59 loc) · 2.15 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
const ProductType = {
SOCKS: 'socks',
HAT: 'hat',
};
const ProductColor = {
RED: 'red',
GREEN: 'green',
BLUE: 'blue',
WHITE: 'white',
};
const data = [
{'type': 'socks', 'color': 'red', 'quantity': 10, 'priceForPair': '$3'},
{'type': 'socks', 'color': 'green', 'quantity': 5, 'priceForPair': '$10'},
{'type': 'socks', 'color': 'blue', 'quantity': 8, 'priceForPair': '$6'},
{'type': 'hat', 'color': 'red', 'quantity': 7, 'price': '$5'},
{'type': 'hat', 'color': 'green', 'quantity': 0, 'price': '$6'},
{'type': 'socks', 'color': 'blue', 'priceForPair': '$6'},
{'type': 'socks', 'color': 'red', 'quantity': 10, 'priceForPair': '$3'},
{'type': 'socks', 'color': 'white', 'quantity': 3, 'priceForPair': '$4'},
{'type': 'socks', 'color': 'green', 'priceForPair': '$10'},
{'type': 'socks', 'color': 'blue', 'quantity': 2, 'priceForPair': '$6'},
{'type': 'hat', 'color': 'green', 'quantity': 3, 'price': '$5'},
{'type': 'hat', 'color': 'red', 'quantity': 1, 'price': '$6'},
{'type': 'socks', 'color': 'blue', 'priceForPair': '$6'},
];
const getQuantity = (item) => item.quantity ? item.quantity : 0;
const calculatePrice = (quantity, price) => quantity * price.substring(1);
const calculateTotal = (obj) => Object.values(obj).reduce((a, b) => a + b);
const print = (socks, hats, colors) => {
console.log(`Socks - ${socks} pairs`);
console.log(`Red Hats - ${hats} items`);
console.log(`Red - $${colors.red}, Green - $${colors.green}, Blue - $${colors.blue}, White - $${colors.white}`);
console.log(`Total price - $${calculateTotal(colors)}`);
};
const process = () => {
let socks = 0;
let hats = 0;
const colors = {
red: 0,
green: 0,
blue: 0,
white: 0,
};
data.forEach((item) => {
const quantity = getQuantity(item);
switch (item.type) {
case ProductType.SOCKS:
socks += quantity;
colors[item.color] += calculatePrice(quantity, item.priceForPair);
break;
case ProductType.HAT:
hats += item.color === ProductColor.RED ? quantity : 0;
colors[item.color] += calculatePrice(quantity, item.price);
break;
}
});
print(socks, hats, colors);
}
process();