Advertisement
Threed90

Hell's kitchen

Jan 26th, 2022
818
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 tokens = JSON.parse(document.querySelector('#inputs textarea').value).map(e => {
  6.          return e.split(/(?:\s-\s)|(?:,\s)|\s/g);
  7.       });
  8.  
  9.       let restaurants = {};
  10.  
  11.       for (let i = 0; i < tokens.length; i++) {
  12.          
  13.          let name = tokens[i][0];
  14.          if(!restaurants[name]){
  15.             restaurants[name] = {
  16.                workers : [],
  17.                total : 0
  18.             };
  19.          }
  20.  
  21.          for (let j = 1; j < tokens[i].length; j+=2) {
  22.            
  23.             let currentWorker = {
  24.                name : tokens[i][j],
  25.                salary : Number(tokens[i][j+1])
  26.             };
  27.             restaurants[name].workers.push(currentWorker);
  28.             restaurants[name].total += currentWorker.salary;
  29.          }
  30.       }
  31.  
  32.       let restName = "";
  33.       let maxAvg = Number.NEGATIVE_INFINITY;
  34.       for (const key in restaurants) {
  35.          let avg = restaurants[key].total / restaurants[key].workers.length;
  36.  
  37.          if(maxAvg < avg){
  38.             maxAvg = avg;
  39.             restName = key;
  40.          }
  41.       }
  42.  
  43.       restaurants[restName].workers.sort((a,b)=> b.salary - a.salary);
  44.       let bestRestaurantResult = `Name: ${restName} Average Salary: ${maxAvg.toFixed(2)} Best Salary: ${restaurants[restName].workers[0].salary.toFixed(2)}`;
  45.       let bestWorkers = restaurants[restName].workers.map(w => `Name: ${w.name} With Salary: ${w.salary}`).join(' ');
  46.  
  47.       let bestRestElement = document.querySelector('#bestRestaurant p');
  48.  
  49.       bestRestElement.textContent = bestRestaurantResult;
  50.  
  51.       document.querySelector('#workers p').textContent = bestWorkers;
  52.    }
  53. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement