svephoto

Furniture [JavaScript]

Jun 3rd, 2021 (edited)
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function furniture(data) {
  2.     let pattern = new RegExp(">>(?<furniture>[\\w]+)<<(?<price>[\\d]+[.]?[\\d]+)!(?<quantity>[\\d]+)");
  3.     let command = data.shift();
  4.     let furniture = [];
  5.     let currMoney = 0;
  6.     let moneyTotal = 0;
  7.     let quantity = 0;
  8.      
  9.     while (command !== 'Purchase') {
  10.         let match = pattern.exec(command);
  11.      
  12.         if (match !== null) {
  13.             furniture.push(match.groups['furniture']);
  14.             currMoney = Number(match.groups['price']);
  15.             quantity = Number(match.groups['quantity']);
  16.             moneyTotal += currMoney * quantity;
  17.         }
  18.      
  19.         command = data.shift();
  20.     }
  21.  
  22.     console.log('Bought furniture:');
  23.    
  24.     if (furniture.length > 0) {
  25.         console.log(furniture.join('\n'));
  26.     }
  27.  
  28.     console.log(`Total money spend: ${moneyTotal.toFixed(2)}`);
  29. }
  30.  
Add Comment
Please, Sign In to add comment