-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.barIncome.js
More file actions
33 lines (27 loc) · 1.04 KB
/
03.barIncome.js
File metadata and controls
33 lines (27 loc) · 1.04 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
function barIncome(input) {
let totalSum = 0;
for (let line of input) {
let pattern =
/%(?<customer>[A-Z][a-z]+)%[^|$%.]*<(?<product>[\w]+)>[^|$%.]*\|(?<count>[\d]+)\|[^|$%.]*?(?<price>[\d]+[.]?[\d]+?)\$/gm;
let order = pattern.exec(line);
if (order) {
let customer = order.groups.customer;
let product = order.groups.product;
let count = order.groups.count;
let price = order.groups.price;
totalSum += count * price;
console.log(`${customer}: ${product} - ${(count * price).toFixed(2)}`);
}
if (line == "end of shift") {
console.log(`Total income: ${totalSum.toFixed(2)}`);
}
}
}
barIncome([
"%George%<Croissant>|2|10.3$",
"%Peter%<Gum>|1|1.3$",
"%Maria%<Cola>|1|2.4$",
'%Valid%<Valid>valid|10|valid20$',
"end of shift",
]);
///%(?<customer>[A-Z][a-z]+)%<(?<product>[\w]+)>\|(?<count>[\d]+)\|(?<price>[\d]+[.][\d]+)\$/gm /%(?<customer>[A-Z][a-z]+)%[^|$%.]*<(?<product>[\w]+)>[^|$%.]*\|(?<count>[\d]+)\|[^|$%.]*(?<price>[\d]+[.]?[\d]+?)\$/gm