Advertisement
BoyanGyurov

07. Hells Kitchen

Sep 23rd, 2022
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     document.querySelector('#btnSend').addEventListener('click', onClick);
  3.  
  4.     function onClick() {
  5.         let input = JSON.parse(document.querySelector('textarea').value);
  6.         let bestRestaurantOutput = document.querySelector('#bestRestaurant p');
  7.         let bestWorkersOutput = document.querySelector('#workers p');
  8.  
  9.         let restaurant = [0, 0, 0, 0];
  10.  
  11.         input.forEach(line => {
  12.             line = line.match(/[^-,\s]+/g);
  13.             let restaurantName = line.shift();
  14.             let workers = line.filter(e => isNaN(e));
  15.             let salaries = line.filter(e => !isNaN(e));
  16.  
  17.             if (restaurant[0] === restaurantName) {
  18.                 restaurant[2].push(...workers);
  19.                 restaurant[3].push(...salaries);
  20.                 restaurant[1] = calcAverageSalary(restaurant[3]);
  21.             } else {
  22.                 let averageSalary = calcAverageSalary(salaries);
  23.                 if (averageSalary > restaurant[1]) {
  24.                     restaurant[0] = restaurantName;
  25.                     restaurant[1] = averageSalary;
  26.                     restaurant[2] = workers;
  27.                     restaurant[3] = salaries;
  28.                 }
  29.             }
  30.         });
  31.  
  32.         function calcAverageSalary(salaries) {
  33.             return salaries.reduce((a, b) => Number(a) + Number(b)) / salaries.length;
  34.         }
  35.  
  36.         function calcHighestSalary() {
  37.             return Math.max.apply(null, restaurant[3]);
  38.         }
  39.  
  40.         function sortSalaries() {
  41.             let workersAndSalaries = {};
  42.             restaurant[2].forEach((e, i) => {
  43.                 workersAndSalaries[e] = restaurant[3][i];
  44.             });
  45.             let sorted = Object.entries(workersAndSalaries).sort((a, b) => b[1] - a[1]);
  46.             print(sorted);
  47.         }
  48.         sortSalaries();
  49.  
  50.         function print(sorted) {
  51.             bestRestaurantOutput.textContent =
  52.                 `Name: ${restaurant[0]} Average Salary: ${(restaurant[1]).toFixed(2)} Best Salary: ${(calcHighestSalary()).toFixed(2)}`
  53.             sorted.forEach(([name, salary]) => bestWorkersOutput.textContent += `Name: ${name} With Salary: ${salary} `);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement