Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let carsCount = +input.shift()
- let carsObj = {}
- for (let i = 0; i < carsCount; i++) {
- let car = input.shift().split('|')
- let [model, mileage, fuel] = car
- carsObj[model] = {
- mileage: +mileage,
- fuel: +fuel
- }
- }
- let line = input.shift()
- while (line !== 'Stop') {
- line = line.split(' : ')
- let [cmd, car, km, fuel] = line
- if (cmd == 'Drive') {
- if (carsObj[car].fuel < fuel) {
- console.log("Not enough fuel to make that ride");
- }
- else {
- carsObj[car].mileage += +km;
- carsObj[car].fuel -= +fuel;
- console.log(`${car} driven for ${km} kilometers. ${fuel} liters of fuel consumed.`);
- }
- if (carsObj[car].mileage >= 100000) {
- console.log(`Time to sell the ${car}!`);
- delete carsObj[car];
- }
- }
- else if (cmd == 'Refuel') {
- carsObj[car].fuel += +km;
- let diff = km
- if (carsObj[car].fuel > 75) {
- diff = 75 - (carsObj[car].fuel-km);
- carsObj[car].fuel = 75;
- }
- console.log(`${car} refueled with ${diff} liters`);
- }
- else if (cmd == 'Revert') {
- carsObj[car].mileage -= +km;
- if (carsObj[car].mileage < 10000) {
- carsObj[car].mileage = 10000
- }
- else {
- console.log(`${car} mileage decreased by ${km} kilometers`); //${km} ???
- }
- }
- line = input.shift() //last
- }
- let arr = Object.entries(carsObj)
- arr.sort((a,b)=>b[1].mileage - a[1].mileage || a[0].localeCompare(b[0]))
- arr.forEach(element => {
- console.log(`${element[0]} -> Mileage: ${element[1].mileage} kms, Fuel in the tank: ${element[1].fuel} lt.`);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement