Advertisement
-Annie-

CityMarkets - Exam Problem

Jun 8th, 2017
106
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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement