Advertisement
teofarov13

Untitled

Mar 19th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bar(arr) {
  2.     let pattern = /%(?<customer>[A-Z][a-z]*)%[^|$%.]*?<(?<product>\w+)>[^|$%.]*?\|(?<count>\d+)\|[^|$%.]*?(?<price>[0-9]+(\.[0-9]+)?)\$/;
  3.    
  4.     let totalIncome = 0;
  5.     let name;
  6.     let product;
  7.     //let list = {};
  8.     for (let line of arr) {
  9.         if (line === 'end of shift') {
  10.             break;
  11.         }
  12.         if (pattern.test(line)) {
  13.             let totalPrice = 0;
  14.             let tokens = pattern.exec(line);
  15.             name = tokens.groups.customer;
  16.             product = tokens.groups.product;
  17.             let price = Number(tokens.groups.price) * Number(tokens.groups.count);
  18.             totalPrice = price;
  19.             totalIncome+=totalPrice
  20.             console.log(`${name}: ${product} - ${totalPrice.toFixed(2)}`);
  21.             price=0
  22.         }
  23.     }
  24.     console.log(`Total income: ${totalIncome.toFixed(2)}`);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement