-Annie-

CityMarkets SortedByName

Jun 10th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let summary = new Map();
  3.  
  4.     for(let row of input) {
  5.         let [town, product, sales] = row.split(' -> ');
  6.         sales = sales.split(' : ').reduce((a, b) => a * b);
  7.  
  8.         if(!summary.has(town)) {
  9.             summary.set(town, new Map());
  10.         }
  11.  
  12.         if(!summary.get(town).has(product)) {
  13.             summary.get(town).set(product, 0);
  14.         }
  15.  
  16.         let oldSales = summary.get(town).get(product);
  17.         summary.get(town).set(product, oldSales + sales)
  18.     }
  19.  
  20.     //for(let [town, products] of summary) {
  21.     //   console.log(`Town - ${town}`);
  22.     //      for(let [product, sales] of products) {
  23.     //         console.log(`$$$${product} : ${sales}`);
  24.     //      }
  25.     //}
  26.  
  27.     let sortedTowns = [...summary.entries()].sort(mySort);
  28.  
  29.     for(let [town, productsMap] of sortedTowns) {
  30.         console.log(town);
  31.         for(let [name, price] of productsMap) {
  32.             console.log(`${name} => ${price}`);
  33.         }
  34.     }
  35.  
  36.     function mySort(a, b) {
  37.         let aLength = a[0].length;
  38.         let bLength = b[0].length;
  39.  
  40.         return bLength - aLength;
  41.     }
  42. }
  43.  
  44. solve(['Sofia -> Laptops HP -> 200 : 2000',
  45.     'Sofia -> Raspberry -> 200000 : 1500',
  46.     'Sofia -> Audi Q7 -> 200 : 100000',
  47.     'Montana -> Portokals -> 200000 : 1',
  48.     'Montana -> Qgodas -> 20000 : 0.2',
  49.     'Montana -> Chereshas -> 1000 : 0.3']);
Add Comment
Please, Sign In to add comment