Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(vignettes) {
- let towns = [];
- vignettes.forEach(x => {
- let currentTown = towns.find(a => a.townName === x.town);
- if (!currentTown) {
- currentTown = {
- townName: x.town,
- registerVignets: 1,
- profit: x.price,
- cars: []
- };
- towns.push(currentTown);
- } else {
- currentTown.registerVignets++;
- currentTown.profit += x.price;
- }
- let currentCar = currentTown.cars.find(a => a.model === x.model);
- if (!currentCar) {
- currentCar = {
- model: x.model,
- registeredCars: 1,
- highiestPrice: x.price,
- plates: []
- };
- currentCar.plates.push(x.regNumber);
- currentTown.cars.push(currentCar);
- } else {
- currentCar.registeredCars++;
- currentCar.plates.push(x.regNumber);
- if (currentCar.highiestPrice < x.price) {
- currentCar.highiestPrice = x.price;
- }
- }
- });
- towns = towns.sort((a, b) => b.profit - a.profit
- || b.registerVignets - a.registerVignets
- || a.townName.localeCompare(b.townName));
- let mostProfitableTown = towns[0];
- mostProfitableTown.cars = mostProfitableTown.cars.sort((a, b) =>
- b.registeredCars - a.registeredCars
- || b.highiestPrice - a.highiestPrice
- || a.model.localeCompare(b.model));
- console.log(`${mostProfitableTown.townName} is most profitable - ${mostProfitableTown.profit} BGN`);
- let bestCar = mostProfitableTown.cars[0];
- console.log(`Most driven model: ${bestCar.model}`)
- let result = towns.filter(x => x.cars.some(a => a.model === bestCar.model)).filter((x => x))
- .map(x => {
- return {
- townName: x.townName,
- plates: x.cars.filter(z => z.model === bestCar.model)
- .map(g => g.plates
- .sort((a, b) => a.localeCompare(b))
- .join(', '))
- }
- });
- result.forEach(x => {
- console.log(`${x.townName}: ${x.plates}`)
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement