Advertisement
kstoyanov

04. City Markets

Sep 22nd, 2020 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cityMarkets(arr) {
  2.   const markets = {};
  3.  
  4.   arr.forEach((str) => {
  5.     const [town, product, amountOfSales, priceForOneUnit] = str.split(/[^A-z0-9.\s+]+/g);
  6.  
  7.     const salesIncome = Number(amountOfSales) * Number(priceForOneUnit);
  8.  
  9.     if (!Object.prototype.hasOwnProperty.call(markets, town)) {
  10.       markets[town] = [];
  11.       const obj = { product: product.trim(), money: salesIncome };
  12.       markets[town].push(obj);
  13.     } else {
  14.       const obj = { product: product.trim(), money: salesIncome };
  15.       markets[town].push(obj);
  16.     }
  17.   });
  18.  
  19.   Object.entries(markets).forEach((town) => {
  20.     const [townName, products] = town;
  21.  
  22.     console.log(`Town - ${townName}`);
  23.  
  24.     products.forEach((store) => {
  25.       const { product, money } = store;
  26.  
  27.       console.log(`$$$${product} : ${money}`);
  28.     });
  29.   });
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement