Advertisement
ErolKZ

Untitled

Nov 21st, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. function solve(input) {
  2.  
  3. let pattern = /(?<custm>\%[A-Z][a-z]+\%).*(?<prod>\<\w+\>).*(?<count>\|\d+\|)[^\|\$\.\%]*?(?<price>\d+[\.]\d+\$|\d+\$)/;
  4.  
  5. let i = 0;
  6.  
  7. let obj = {};
  8.  
  9. let income = 0;
  10.  
  11.  
  12.  
  13. while (input[i] !== 'end of shift') {
  14.  
  15. if ((customer = pattern.exec(input[i]))) {
  16.  
  17. let name = customer.groups.custm.split('').filter(el => /[A-Z]|[a-z]/.test(el));
  18.  
  19. name = name.join('');
  20.  
  21. let product = customer.groups.prod.split('').filter(el => /[A-Z]|[a-z]/.test(el));
  22.  
  23. product = product.join('');
  24.  
  25. let count = customer.groups.count.split('').filter(el => /\d/.test(el));
  26.  
  27. count = count.join('');
  28.  
  29. let price = customer.groups.price.split('$')[0];
  30.  
  31.  
  32. income += count * price;
  33.  
  34. if (obj.hasOwnProperty(name)) {
  35.  
  36. obj[name][product] = count * price;
  37.  
  38. } else {
  39.  
  40. obj[name] = { [product]: count * price };
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47.  
  48. i++;
  49.  
  50. }
  51.  
  52.  
  53. // console.log(obj, income);
  54.  
  55.  
  56. for (let key in obj) {
  57.  
  58. for (let key2 in obj[key]) {
  59.  
  60. console.log(`${key}: ${key2} - ${obj[key][key2].toFixed(2)}`);
  61.  
  62. }
  63.  
  64. }
  65.  
  66. console.log(`Total income: ${income.toFixed(2)}`);
  67.  
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement