Aliendreamer

e-venetka

Feb 11th, 2019
228
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.            };
  14.           townNeeded.car.push(newObjForTown);
  15.           townNeeded.totalPrice+=Number(current.price);
  16.           townNeeded.totalCar+=1;
  17.        }
  18.        else{
  19.            let newTown={
  20.                'car':[],
  21.                'totalPrice':0,
  22.                'totalCar':0,
  23.                'name':current.town
  24.            };
  25.                let newCar={
  26.                    'model':current.model,
  27.                    'regNumber':current.regNumber,
  28.                    'price':Number(current.price)
  29.                }
  30.    
  31.               newTown.car.push(newCar);
  32.               newTown.totalPrice+=Number(current.price);
  33.               newTown.totalCar+=1;  
  34.               townList.push(newTown);
  35.        }
  36.     }
  37.    
  38.     let sortedTowns =townList.sort((a,b)=>b.totalPrice - a.totalPrice || b.car.length - a.car.length || a.name.localeCompare(b.name));
  39.    
  40.     let mostUsedCar=GetMostUsedCar(sortedTowns[0]);
  41.  
  42.     function GetMostUsedCar(town){
  43.  
  44.         let array=town.car.map(x=>x.model);
  45.         var counts = {};
  46.         var compare = 0;
  47.         var mostFrequent;
  48.         for(var i = 0, len = array.length; i < len; i++){
  49.             var word = array[i];
  50.            
  51.             if(counts[word] === undefined){
  52.                 counts[word] = 1;
  53.             }else{
  54.                 counts[word] = counts[word] + 1;
  55.             }
  56.             if(counts[word] > compare){
  57.                   compare = counts[word];
  58.                   mostFrequent = array[i];
  59.             }
  60.          }
  61.        return mostFrequent;
  62.     }
  63.     let firstTown=sortedTowns[0];
  64.     console.log(`${firstTown.name} is most profitable - ${firstTown.totalPrice} BGN`);
  65.     console.log(`Most driven model: ${mostUsedCar}`);
  66.     let printTowns=sortedTowns
  67.     .sort((a,b)=>a.name.localeCompare(b.name))
  68.     .filter(x=>x.car.some(c=>c.model===mostUsedCar))
  69.     .filter(x=>x)
  70.     .map(x=>{
  71.         return {
  72.             "townName":x.name,
  73.             "plates":x.car.filter(x=>x.model===mostUsedCar)
  74.             .map(x=>x.regNumber)
  75.             .sort((a,b)=>a.localeCompare(b))
  76.             .join(', ')
  77.         }
  78.     });
  79.     printTowns.forEach(x=>console.log(`${x.townName}: ${x.plates}`));
  80. }
Advertisement
Add Comment
Please, Sign In to add comment