ErolKZ

Untitled

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