Advertisement
GeorgiLukanov87

08. Hells Kitchen

Mar 21st, 2023 (edited)
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 08. Hells Kitchen
  2. // DOM and Events - More Exercises
  3. // https://judge.softuni.org/Contests/Practice/Index/3796#7
  4.  
  5. // variant 1
  6. function solve() {
  7.     document.querySelector('#btnSend').addEventListener('click', onClick);
  8.  
  9.     function onClick() {
  10.         let arr = JSON.parse(document.querySelector('#inputs textarea').value);
  11.         let objWinner = findBestRestaurant(arr);
  12.         document.querySelector('#bestRestaurant>p').textContent = getMsgRest(objWinner);
  13.         document.querySelector('#workers>p').textContent = getMsgEmp(objWinner.workers);
  14.     }
  15.  
  16.     function getMsgRest(objWinner) {
  17.         return `Name: ${objWinner.name} Average Salary: ${objWinner.avgSalary.toFixed(2)} Best Salary: ${objWinner.maxSalary.toFixed(2)}`;
  18.     }
  19.  
  20.     function getMsgEmp(workers) {
  21.         return workers.map(w => `Name: ${w.worker} With Salary: ${w.salary}`).join(' ');
  22.     }
  23.  
  24.     function findBestRestaurant(arr) {
  25.         let resultRestaurants = arr.reduce((acc, e) => {
  26.             let [restaurant, ...workers] = e.split(/(?: - )|(?:, )/g);
  27.             workers = workers.map(w => {
  28.                 let [worker, salary] = w.split(' ');
  29.                 return {
  30.                     worker: worker,
  31.                     salary: +salary
  32.                 };
  33.             });
  34.             let foundRestraunt = acc.find(r => r.name === restaurant);
  35.             if (foundRestraunt) {
  36.                 foundRestraunt.workers = foundRestraunt.workers.concat(workers);
  37.             } else {
  38.                 acc.push({
  39.                     name: restaurant,
  40.                     workers: workers
  41.                 });
  42.             }
  43.             return acc;
  44.         }, []);
  45.  
  46.         resultRestaurants.forEach((el, idx) => {
  47.             el.inputOrder = idx;
  48.             el.avgSalary = el.workers.reduce((acc, el) => acc + el.salary, 0) / el.workers.length;
  49.             el.maxSalary = Math.max(...el.workers.map(w => w.salary));
  50.         });
  51.  
  52.         resultRestaurants.sort((a, b) => b.avgSalary - a.avgSalary || a.inputOrder - b.inputOrder);
  53.         let bestRestaurant = resultRestaurants[0];
  54.         bestRestaurant.workers.sort((a, b) => b.salary - a.salary);
  55.  
  56.         return bestRestaurant;
  57.     }
  58. }
  59.  
  60. // variant 2
  61.  
  62. function solve() {
  63.    document.querySelector('#btnSend').addEventListener('click', onClick);
  64.  
  65.    function onClick() {
  66.       let input = JSON.parse(document.querySelector('textarea').value);
  67.       let bestRestaurantOutput = document.querySelector('#bestRestaurant p');
  68.       let bestWorkersOutput = document.querySelector('#workers p');
  69.  
  70.       let restaurant = [0, 0, 0, 0];
  71.  
  72.       input.forEach(line => {
  73.          line = line.match(/[^-,\s]+/g);
  74.          let restaurantName = line.shift();
  75.          let workers = line.filter(e => isNaN(e));
  76.          let salaries = line.filter(e => !isNaN(e));
  77.  
  78.          if (restaurant[0] === restaurantName) {
  79.             restaurant[2].push(...workers);
  80.             restaurant[3].push(...salaries);
  81.             restaurant[1] = calcAverageSalary(restaurant[3]);
  82.          } else {
  83.             let averageSalary = calcAverageSalary(salaries);
  84.             if (averageSalary > restaurant[1]) {
  85.                restaurant[0] = restaurantName;
  86.                restaurant[1] = averageSalary;
  87.                restaurant[2] = workers;
  88.                restaurant[3] = salaries;
  89.             }
  90.          }
  91.       });
  92.  
  93.       function calcAverageSalary(salaries) {
  94.          return salaries.reduce((a, b) => Number(a) + Number(b)) / salaries.length;
  95.       }
  96.       function calcHighestSalary() {
  97.          return Math.max.apply(null, restaurant[3]);
  98.       }
  99.       function sortSalaries() {
  100.          let workersAndSalaries = {};
  101.          restaurant[2].forEach((e, i) => {
  102.             workersAndSalaries[e] = restaurant[3][i];
  103.          });
  104.          let sorted = Object.entries(workersAndSalaries).sort((a, b) => b[1] - a[1]);
  105.          print(sorted);
  106.       }
  107.       sortSalaries();
  108.  
  109.       function print(sorted) {
  110.          bestRestaurantOutput.textContent =
  111.             `Name: ${restaurant[0]} Average Salary: ${(restaurant[1]).toFixed(2)} Best Salary: ${(calcHighestSalary()).toFixed(2)}`
  112.          sorted.forEach(([name, salary]) => bestWorkersOutput.textContent += `Name: ${name} With Salary: ${salary} `);
  113.       }
  114.    }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement