-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelp the bookseller !.js
74 lines (62 loc) · 2.24 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
73
74
//https://www.codewars.com/kata/54dc6f5a224c26032800005c/train/javascript
/**
*
* A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code c of 3, 4, 5 or more characters. The 1st character of a code is a capital letter which defines the book category.
In the bookseller's stocklist each code c is followed by a space and by a positive integer n (int n >= 0) which indicates the quantity of books of this code in stock.
For example an extract of a stocklist could be:
L = {"ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"}.
or
L = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"] or ....
*/
function stockList(listOfArt, listOfCat){
let tempArr = [],
checkEmpty = 0;
for(const letter of listOfCat){
let t = 0;
for(const article of listOfArt){
if (letter === article[0]){
t += parseInt(article.slice(article.indexOf(" ")+1, article.length));
}
}
console.log("T is "+t);
console.log("tempArr is "+tempArr)
tempArr.push(`(${letter} : ${t})`);
checkEmpty += t;
}
if(!checkEmpty){
return "";
}
console.log(tempArr)
return tempArr.join(" - ")
}
// console.log(listOfArt[0].match("z"))
// function matchLetter(letter){
// let nums = 0;
// let tempArr = [];
// for(let i = 0; i <listOfArt.length; i++){
// console.log("nums is "+nums)
// if(listOfArt[i].match(letter)){
// // let letterAt = listOfArt[i].match(letter).index;
// let spaceAt = listOfArt[i].indexOf(" ");
// console.log("space at "+spaceAt)
// nums = nums + parseInt( listOfArt[i].slice(spaceAt+1, listOfArt[i].length));
// console.log(listOfArt[i].slice(spaceAt+1, listOfArt[i].length))
// console.log("nums is "+nums)
// tempArr.push(letter, nums);
// }
// console.log("nums is "+nums)
// nums = 0;
// }
// console.log(tempArr)
// }
// let resArr = [];
// let sum = 0;
// for(const word of listOfCat){
// console.log(word);
// if(matchLetter(word)){
// sum+=matchLetter(word);
// }
// console.log("sum is "+sum+" letter is "+word);
// sum = 0;
// }
// matchLetter("A")