Advertisement
TZinovieva

SoftUni Bar Income JS Fundamentals

Mar 20th, 2023
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function softUniBarIncome(lines) {
  2.     let command = lines.pop();
  3.    
  4.     let totalIncome = 0;
  5.     let pattern = /%(?<customer>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<count>\d+)\|[^|$%\.0-9]*(?<price>[0-9]+.?\d*)\$/g;
  6.     while((data = pattern.exec(lines)) !== null) {
  7.         let customer = data.groups['customer'];
  8.         let product = data.groups['product'];
  9.         let quantity = data.groups['count'];
  10.         let price = data.groups['price'];
  11.         let orderPrice = quantity * price;
  12.  
  13.         console.log(`${customer}: ${product} - ${orderPrice.toFixed(2)}`);
  14.         totalIncome += orderPrice;
  15.     }
  16.  
  17.     if (command === 'end of shift') {
  18.         console.log(`Total income: ${totalIncome.toFixed(2)}`);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement