Advertisement
vladovip

RegExp_Bar Income_JS FUND

Sep 4th, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function softUniBarIncome (inputArr) {
  2.  
  3.     let currentProductInfo = inputArr.shift();
  4.     let pattern = /%(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<count>\d+)\|([\D])*(?<price>\d+(\.\d+)?)\$/g;
  5.     let totalSum = 0;
  6.  
  7.     while (currentProductInfo !== "end of shift") {
  8.        
  9.     let allMatchArr = pattern.exec(currentProductInfo);
  10.     // console.log(allMatchArr);
  11.      while ( allMatchArr !== null){
  12.         // console.log(allMatchArr);
  13.       let customerName = allMatchArr[1];
  14.       let productName = allMatchArr[2];
  15.       let countProduct = Number(allMatchArr[3]);
  16.       let productPrice = Number(allMatchArr[5]);
  17.       let totalpricePerProduct = countProduct * productPrice;
  18.       totalSum += Number(totalpricePerProduct);
  19.       console.log(`${customerName}: ${productName} - ${totalpricePerProduct.toFixed(2)}`);
  20.  
  21.       allMatchArr = pattern.exec(currentProductInfo);
  22.     }
  23.  
  24.     currentProductInfo = inputArr.shift();
  25.     }
  26.  
  27.     console.log(`Total income: ${totalSum.toFixed(2)}`);
  28.  
  29. }
  30.  
  31.  
  32.  
  33. softUniBarIncome (['%George%<Croissant>|2|10.3$',
  34. '%Peter%<Gum>|1|1.3$',
  35. '%Maria%<Cola>|1|2.4$',
  36. 'end of shift']
  37. );
  38.  
  39.  
  40. console.log(`*********`);
  41.  
  42.  
  43. softUniBarIncome (['%InvalidName%<Croissant>|2|10.3$',
  44. '%Peter%<Gum>1.3$',
  45. '%Maria%<Cola>|1|2.4',
  46. '%Valid%<Valid>valid|10|valid20$',
  47. 'end of shift']
  48. );
  49.  
  50.  
  51.  
  52. // %(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<count>\d+)\|([^\d])*(?<price>\d+(\.\d+)?)\$
  53.  
  54. // %(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<count>\d+)\|([\D])*(?<price>\d+(\.\d+)?)\$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement