vladovip

RegExp - Furniture_ JS FUND

Sep 4th, 2022
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let pattern = />>(?<furniture>[A-Z][a-zA-Z]+)<<(?<price>\d+\.?\d+)!(?<quantity>\d+)/g;
  4.     let match = pattern.exec(input);
  5.     let furniture = [];
  6.     let totalPrice = 0;
  7.  
  8.     while (match != null) {
  9.  
  10.         let price = match.groups['price'];
  11.         let quantity = match.groups['quantity'];
  12.  
  13.         let currentCost = price * quantity;
  14.         furniture.push(match.groups['furniture']);
  15.         totalPrice += currentCost;
  16.  
  17.         match = pattern.exec(input);
  18.     }
  19.  
  20.    
  21.     if (furniture.length == 0) {
  22.         console.log(`Bought furniture:\nTotal money spend: ${totalPrice.toFixed(2)}`);
  23.     } else {
  24.         console.log("Bought furniture:")
  25.         for (let object of furniture) {
  26.             console.log(object);
  27.         }
  28.         console.log(`Total money spend: ${totalPrice.toFixed(2)}`);
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment