maymunskoa

03. SoftUni Bar Income (regExp)

Mar 18th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.  
  4.     let regEx = /^%(?<name>[A-Z][a-z]*)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<quantity>[0-9]+)\|[^|$%.]*?(?<price>[0-9]+\.*[0-9]*)\$$/
  5.  
  6. let totalIncome = 0;
  7.  
  8.  
  9. for (const line of input) {
  10.     if(line === 'end of shift'){
  11.         break;
  12.     }
  13.     if(line.match(regEx)!==null){
  14.         let result = line.match(regEx).groups;
  15.         let name = result.name;
  16.         let product = result.product;
  17.         let quantity = Number(result.quantity);
  18.         let price = Number(result.price);
  19.        
  20.        
  21.         console.log(`${name}: ${product} - ${(price*quantity).toFixed(2)}`);
  22.        
  23.         totalIncome += price*quantity;
  24.     }
  25.    
  26. }
  27.  
  28.   console.log(`Total income: ${totalIncome.toFixed(2)}`);
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment