Advertisement
ilianrusev

hells kitchen

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