Advertisement
Lulunga

03. SoftUni Bar Income regex

Jul 26th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let totalIncome = 0;
  3.     let index = arr.indexOf('end of shift');
  4.     arr.splice(index, 1);
  5.  
  6.     arr.forEach(line => {
  7.        
  8.         let pattern = /%([A-Z][a-z]+)%[^|,$%]*<(\w+)>[^|,$%]*\|([0-9]+)\|[^|,$%0-9]*([0-9]+\.?\d*)\$/gm;
  9.         let result = pattern.exec(line);
  10.         if (result) {
  11.             let name = result[1];
  12.             let product = result[2];
  13.             let count = Number(result[3]);
  14.             let price = Number(result[4]);
  15.             let currentSum = count * price;
  16.             totalIncome += currentSum;
  17.             console.log(`${name}: ${product} - ${currentSum.toFixed(2)}`);
  18.         }
  19.  
  20.     });
  21.     console.log(`Total income: ${totalIncome.toFixed(2)}`);
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement