kStoikow

Untitled

Oct 15th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let pattern = />>(?<name>\w+)<<(?<price>\d+[.]?\d+)!(?<quantity>\d+)/g
  3.     let totalMoney = 0;
  4.     let furnitures = [];
  5.     let line = input.shift();
  6.  
  7.     while (line != 'Purchase') {
  8.         let result;
  9.         if (line.match(pattern)) {
  10.             result = pattern.exec(line);
  11.             furnitures.push(result.groups.name);
  12.             totalMoney += (result.groups.price * result.groups.quantity);
  13.         }
  14.  
  15.         line = input.shift();
  16.     }
  17.  
  18.     console.log('Bought furniture:');
  19.     console.log(`${furnitures.join('\n')}`);
  20.     console.log(`Total money spend: ${totalMoney.toFixed(2)}`);
  21. }
  22. solve(['>>Sofa<<100.22!3',
  23.     '>>TV<<200.1055!2',
  24.     '>Invalid<<!5',
  25.     'Purchase',
  26. ])
Advertisement
Add Comment
Please, Sign In to add comment