Advertisement
Guest User

nfsas

a guest
Jul 7th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function needForSpeed(input = []) {
  2.     let numCars = Number(input.shift())
  3.     let cars = input.splice(0, numCars);
  4.     let objCars = {};
  5.  
  6.     for (const car of cars) {
  7.         let [carModel, mileage, fuel] = car.split('|');
  8.         mileage = Number(mileage);
  9.         fuel = Number(fuel);
  10.  
  11.         if (!objCars.hasOwnProperty(carModel)) {
  12.             objCars[carModel] = { mileage: mileage, fuel: fuel }
  13.         }
  14.     }
  15.  
  16.     let betterInput = input.splice(0, input.indexOf('Stop'));
  17.  
  18.     for (const line of betterInput) {
  19.         let [command, carName, value, value2] = line.split(' : ');
  20.         value = Number(value);
  21.         value2 = Number(value2);
  22.  
  23.         switch (command) {
  24.             case 'Drive':
  25.                 let distance = value;
  26.                 let fuel = value2;
  27.  
  28.                 if (fuel > objCars[carName].fuel) {
  29.                     console.log("Not enough fuel to make that ride");
  30.                 } else if (objCars[carName].fuel > fuel) {
  31.                     objCars[carName].fuel -= fuel;
  32.                     objCars[carName].mileage += distance;
  33.                     console.log(`${carName} driven for ${distance} kilometers. ${fuel} liters of fuel consumed.`);
  34.  
  35.                     if (objCars[carName].mileage >= 100_000) {
  36.                         console.log(`Time to sell the ${carName}!`);
  37.                         delete objCars[carName];
  38.                     }
  39.                 }
  40.                 break;
  41.  
  42.             case 'Refuel':
  43.                 let fuelForRefill = value;
  44.  
  45.                 if (objCars[carName].fuel + fuelForRefill >= 75) {
  46.                     console.log(`${carName} refueled with ${75 - objCars[carName].fuel} liters`);
  47.                     objCars[carName].fuel = 75;
  48.                 } else {
  49.                     objCars[carName].fuel += fuelForRefill;
  50.                     console.log(`${carName} refueled with ${fuelForRefill} liters`);
  51.                 }
  52.                 break;
  53.  
  54.             case 'Revert':
  55.                 let KMs = value;
  56.  
  57.                 if (objCars[carName].mileage - KMs <= 10_000) {
  58.                     objCars[carName].mileage = 10_000;
  59.                 } else {
  60.                     objCars[carName].mileage -= KMs;
  61.                     console.log(`${carName} mileage decreased by ${KMs} kilometers`)
  62.                 }
  63.                 break;
  64.         }
  65.     }
  66.    
  67.     function sortByMileage(a, b) {
  68.         a = a.mileage;
  69.         b = b.mileage;
  70.  
  71.         let comp = 0;
  72.  
  73.         if (b > a) {
  74.             comp = 1;
  75.         } else {
  76.             comp = -1
  77.         }
  78.  
  79.         return comp
  80.     }
  81.  
  82.     function sortAlphabetically(a, b) {
  83.         a = arr[0][0];
  84.         b = arr[0][0];
  85.  
  86.         return a.localeCompare(b)
  87.     }
  88.    
  89.     let arr = Object.entries(objCars).sort(sortByMileage || sortAlphabetically);
  90.  
  91.     for (let i = 0; i < arr.length; i++) {
  92.         console.log(`${arr[i][0]} -> Mileage: ${arr[i][1].mileage} kms, Fuel in the tank: ${arr[i][1].fuel} lt.`);
  93.     }
  94. }
  95.  
  96. needForSpeed([
  97.     '4',
  98.     'Lamborghini Veneno|11111|74',
  99.     'Bugatti Veyron|12345|67',
  100.     'Koenigsegg CCXR|67890|12',
  101.     'Aston Martin Valkryie|99900|50',
  102.     'Drive : Koenigsegg CCXR : 382 : 82',
  103.     'Drive : Aston Martin Valkryie : 99 : 23',
  104.     'Drive : Aston Martin Valkryie : 2 : 1',
  105.     'Refuel : Lamborghini Veneno : 40',
  106.     'Revert : Bugatti Veyron : 2000',
  107.     'Stop'
  108. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement