Advertisement
social1986

Untitled

Feb 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(vignettes) {
  2.     let towns = [];
  3.  
  4.     vignettes.forEach(x => {
  5.         let currentTown = towns.find(a => a.townName === x.town);
  6.         if (!currentTown) {
  7.             currentTown = {
  8.                 townName: x.town,
  9.                 registerVignets: 1,
  10.                 profit: x.price,
  11.                 cars: []
  12.             };
  13.             towns.push(currentTown);
  14.         } else {
  15.             currentTown.registerVignets++;
  16.             currentTown.profit += x.price;
  17.         }
  18.  
  19.         let currentCar = currentTown.cars.find(a => a.model === x.model);
  20.  
  21.         if (!currentCar) {
  22.             currentCar = {
  23.                 model: x.model,
  24.                 registeredCars: 1,
  25.                 highiestPrice: x.price,
  26.                 plates: []
  27.             };
  28.             currentCar.plates.push(x.regNumber);
  29.             currentTown.cars.push(currentCar);
  30.         } else {
  31.             currentCar.registeredCars++;
  32.             currentCar.plates.push(x.regNumber);
  33.             if (currentCar.highiestPrice < x.price) {
  34.                 currentCar.highiestPrice = x.price;
  35.             }
  36.         }
  37.     });
  38.     towns = towns.sort((a, b) => b.profit - a.profit
  39.         || b.registerVignets - a.registerVignets
  40.         || a.townName.localeCompare(b.townName));
  41.  
  42.     let mostProfitableTown = towns[0];
  43.  
  44.     mostProfitableTown.cars = mostProfitableTown.cars.sort((a, b) =>
  45.            b.registeredCars - a.registeredCars
  46.         || b.highiestPrice - a.highiestPrice
  47.         || a.model.localeCompare(b.model));
  48.  
  49.     console.log(`${mostProfitableTown.townName} is most profitable - ${mostProfitableTown.profit} BGN`);
  50.  
  51.     let bestCar = mostProfitableTown.cars[0];
  52.     console.log(`Most driven model: ${bestCar.model}`)
  53.  
  54.     let result = towns.filter(x => x.cars.some(a => a.model === bestCar.model)).filter((x => x))
  55.         .map(x => {
  56.             return {
  57.                 townName: x.townName,
  58.                 plates: x.cars.filter(z => z.model === bestCar.model)
  59.                     .map(g => g.plates
  60.                         .sort((a, b) => a.localeCompare(b))
  61.                         .join(', '))
  62.             }
  63.         });
  64.     result.forEach(x => {
  65.         console.log(`${x.townName}: ${x.plates}`)
  66.     });
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement