Advertisement
Aliendreamer

e venetka

Feb 12th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr){
  2.  
  3.     let townList=[];
  4.    
  5.     for(let current of arr){
  6.         let listOfnames=townList.map(x=>x.name);
  7.         let exist=listOfnames.includes(current.town);
  8.        if(exist){
  9.         let townNeeded=townList.find(x=>x.name===current.town);
  10.            let newObjForTown={
  11.                "model":current.model,
  12.                "regNumber":current.regNumber,
  13.                "price":Number(current.price)
  14.            };
  15.           townNeeded.car.push(newObjForTown);
  16.           townNeeded.totalPrice+=Number(current.price);
  17.           townNeeded.totalCar+=1;
  18.        }
  19.        else{
  20.            let newTown={
  21.                'car':[],
  22.                'totalPrice':0,
  23.                'totalCar':0,
  24.                'name':current.town
  25.            };
  26.                let newCar={
  27.                    'model':current.model,
  28.                    'regNumber':current.regNumber,
  29.                    'price':Number(current.price)
  30.                }
  31.    
  32.               newTown.car.push(newCar);
  33.               newTown.totalPrice+=Number(current.price);
  34.               newTown.totalCar+=1;  
  35.               townList.push(newTown);
  36.        }
  37.     }
  38.    
  39.     let sortedTowns =townList.sort((a,b)=>b.totalPrice - a.totalPrice || b.car.length - a.car.length || a.name.localeCompare(b.name));
  40.    
  41.     let mostUsedCar=GetMostUsedCar(sortedTowns[0]);
  42.  
  43.     function GetMostUsedCar(town){
  44.  
  45.         let array=town.car;
  46.         let counted=[]
  47.         for(var i = 0, len = array.length; i < len; i++){
  48.            
  49.             const currentCar=array[i];
  50.             let exist=counted.find(x=>x.model===currentCar.model);
  51.  
  52.             if(exist){
  53.                     exist.count+=1;
  54.             }else{
  55.                 let countedCar={
  56.                     'model':currentCar.model,
  57.                     'regNum':currentCar.regNumber,                  
  58.                     'price':currentCar.price,
  59.                     'count':1
  60.                 }
  61.                 counted.push(countedCar);
  62.             }
  63.          }
  64.          let prices=counted.map(x=>x.price);
  65.          let counts=counted.map(x=>x.count)
  66.          let maxPrice=Math.max(...prices);
  67.          let maxCount=Math.max(...counts);
  68.  
  69.          let carModel=counted
  70.          .filter((a)=>a.count==maxCount)
  71.          .filter((x)=>x.price==maxPrice)
  72.          .sort((a,b)=>a.count-b.count || a.price-b.price ||
  73.          a.model.localeCompare(b.model))[0];                
  74.  
  75.         // let mostFrequent=counted.sort((a,b)=>a.count-b.count || a.price-b.price ||
  76.          //a.model.localeCompare(b.model));
  77.         // let mostFrequent=counted.sort((a,b)=>b.count-a.count || b.price-a.price ||
  78.          //a.model.localeCompare(b.model));
  79.        return carModel.model;
  80.     }
  81.     let firstTown=sortedTowns[0];
  82.     console.log(`${firstTown.name} is most profitable - ${firstTown.totalPrice} BGN`);
  83.     console.log(`Most driven model: ${mostUsedCar}`);
  84.     let printTowns=sortedTowns
  85.     .sort((a,b)=>a.name.localeCompare(b.name))
  86.     .filter(x=>x.car.some(c=>c.model===mostUsedCar))
  87.     .filter(x=>x)
  88.     .map(x=>{
  89.         return {
  90.             "townName":x.name,
  91.             "plates":x.car.filter(x=>x.model===mostUsedCar)
  92.             .map(x=>x.regNumber)
  93.             .sort((a,b)=>a.localeCompare(b))
  94.             .join(', ')
  95.         }
  96.     });
  97.     printTowns.forEach(x=>console.log(`${x.townName}: ${x.plates}`));
  98. }
  99.    
  100.    solve([ { model: 'BMW', regNumber: 'B1234SM', town: 'Varna', price: 2},
  101.    { model: 'BMW', regNumber: 'C5959CZ', town: 'Sofia', price: 8},
  102.    { model: 'Tesla', regNumber: 'NIKOLA', town: 'Burgas', price: 9},
  103.    { model: 'BMW', regNumber: 'A3423SM', town: 'Varna', price: 3},
  104.    { model: 'Lada', regNumber: 'SJSCA', town: 'Sofia', price: 3} ]
  105.    );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement