Advertisement
didkoslawow

Untitled

Feb 6th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function softuniBarIncome(input) {
  2.   let line = input.shift();
  3.   let totalIncome = 0;
  4.   const regExp =
  5.     /%(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<quantity>\d+)\|[^|$%.]*(?<price>\d+(\.\d+)?)\$/gm;
  6.  
  7.   while (line !== 'end of shift') {
  8.     let isValidLine = regExp.exec(line);
  9.  
  10.     if (!isValidLine) {
  11.       line = input.shift();
  12.       continue;
  13.     }
  14.  
  15.     const name = isValidLine.groups.name;
  16.     const product = isValidLine.groups.product;
  17.     const quantity = Number(isValidLine.groups.quantity);
  18.     const price = Number(isValidLine.groups.price);
  19.     const totalProductPrice = quantity * price;
  20.  
  21.     console.log(`${name}: ${product} - ${totalProductPrice.toFixed(2)}`);
  22.     totalIncome += totalProductPrice;
  23.  
  24.     line = input.shift();
  25.   }
  26.  
  27.   console.log(`Total income: ${totalIncome.toFixed(2)}`);
  28. }
  29.  
  30. softuniBarIncome(['%George%<Croissant>|2|10.3$',
  31. '%Peter%<Gum>|1|1.3$',
  32. '%Maria%<Cola>|1|2.4$',
  33. 'end of shift']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement