Advertisement
TZinovieva

Furniture JS Fundamentals

Mar 17th, 2023
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function furniture(arr) {
  2.     let regex = />>(?<furniture>[A-Z]+[a-z]*)<<(?<price>\d+\.*\d*)!(?<quantity>\d+)\b/g;
  3.     let furniture = [];
  4.     let totalPrice = 0;
  5.     while ((data = regex.exec(arr)) !== null) {
  6.         let items = data.groups['furniture'];
  7.         furniture.push(items);
  8.         let price = data.groups['price'];
  9.         let quantity = data.groups['quantity'];
  10.         let itemPrice = price * quantity;
  11.         totalPrice += itemPrice;
  12.     }
  13.     if (furniture.length === 0) {
  14.         console.log(`Bought furniture:`);
  15.         console.log(`Total money spend: ${totalPrice.toFixed(2)}`);
  16.         return;
  17.     }
  18.     console.log(`Bought furniture:\n${furniture.join('\n')}`);
  19.     console.log(`Total money spend: ${totalPrice.toFixed(2)}`);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement