-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
help-the-bookseller.js
72 lines (69 loc) · 2.13 KB
/
help-the-bookseller.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
function stockList(listOfArt, listOfCat){
// a place to keep track of the quantities for each category
const categories = new Map();
// iterate over categories
listOfCat.forEach((category) => {
// set category count to 0
categories.set(category, 0);
});
// iterate over the list of items
listOfArt.forEach((art) => {
// split current item on space to get category code and quantity
const [code, quantity] = art.split(' ');
const [category] = code;
// if the category code exists
if (categories.has(category)) {
// increase quantity at given category based on first letter
categories.set(category, categories.get(category) + Number(quantity));
}
});
// a place to store the output string
let output = '';
const lastCategory = listOfCat.at(-1);
// iterate over the keys+values of the quantities variable
for (const [category, quantity] of categories.entries()) {
// append the formated key+value to the output string
output += `(${category} : ${quantity})`;
// (with a trailing dash if this is not the last key+value)
if (category !== lastCategory) {
output += ' - ';
}
}
return output;
}
function stockList(listOfArt, listOfCat){
if (!listOfArt.length) return '';
const categories = new Map();
listOfCat.forEach((category) => {
categories.set(category, 0);
});
listOfArt.forEach((art) => {
const [[category], quantity] = art.split(' ');
if (categories.has(category)) {
categories.set(category, categories.get(category) + Number(quantity));
}
});
let output = '';
const lastCategory = listOfCat[listOfCat.length - 1];
for (const [category, quantity] of categories.entries()) {
output += `(${category} : ${quantity})`;
if (category !== lastCategory) {
output += ' - ';
}
}
return output;
}
b = ["ABAR 200", "CDXE 500", "BKWR 250", "BTSQ 890", "DRTY 600"]
c = ["A", "B"]
res = "(A : 200) - (B : 1140)"
console.log(
stockList(b, c),
res
);
b = ["CBART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]
c = ["A", "B", "C", "W"]
res = "(A : 0) - (B : 114) - (C : 70) - (W : 0)"
console.log(
stockList(b, c),
res
);