Advertisement
ilianrusev

HellsKitchen

Jan 13th, 2022
770
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.       bestRestaurantOutput.innerHTML = '';
  10.       bestWorkersOutput.innerHTML = ''
  11.  
  12.       let restInfo = {};
  13.       let restaurant = '';
  14.       let avg = 0;
  15.       let currentName = '';
  16.  
  17.       for (let i = 0; i < input.length; i++) {
  18.          restaurant = input[i].split("-")[0].trim();
  19.          let workersInfo = input[i].split(' - ')[1].split(", ");
  20.  
  21.          if (!restInfo[restaurant]) {
  22.             restInfo[restaurant] = {};
  23.          }
  24.  
  25.          for (let j = 0; j < workersInfo.length; j++) {
  26.             let worker = workersInfo[j].split(" ")[0];
  27.             let salary = Number(workersInfo[j].split(" ")[1]);
  28.  
  29.             restInfo[restaurant][worker] = salary;
  30.          }
  31.  
  32.          let arr2 = Object.values(restInfo[restaurant])
  33.          let currentAvg = 0;
  34.  
  35.          for (const el of arr2) {
  36.             currentAvg += el;
  37.          }
  38.  
  39.          currentAvg = (currentAvg / arr2.length)
  40.  
  41.          if (avg < currentAvg) {
  42.             avg = currentAvg
  43.             currentName = restaurant;
  44.          }
  45.       }
  46.  
  47.       let bestSalary = Math.max(...(Object.values(restInfo[currentName])));
  48.  
  49.       Object.entries(restInfo[currentName])
  50.          .sort((a, b) => {
  51.             return b[1] - a[1];
  52.          })
  53.          .forEach((el) => {
  54.             bestWorkersOutput.innerHTML += `Name: ${el[0]} With Salary: ${el[1]} `
  55.          })
  56.  
  57.       bestRestaurantOutput.innerHTML = `Name: ${currentName} Average Salary: ${avg.toFixed(2)} Best Salary: ${bestSalary.toFixed(2)}`;
  58.    }
  59. }
Advertisement
RAW Paste Data Copied
Advertisement