Advertisement
bebo231312312321

Untitled

May 22nd, 2023
84
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 bestList = {}
  6.       let totalSalary = 0
  7.       let avgSalarary = 0
  8.       let currSalary = 0
  9.       let bestWorker = ""
  10.  
  11.       let input = JSON.parse(document.querySelector("#inputs textarea").value)
  12.       for (let line of input) {
  13.          let [restourant, workerInfo] = line.split(" - ")
  14.  
  15.          for (let worker of workerInfo.split(', ')) {
  16.             let [workerName, salaryWorker] = worker.split(' ').map(x => isNaN(x) ? x : Number(x))
  17.             if (!bestList.hasOwnProperty(restourant)) bestList[restourant] = {};
  18.  
  19.             bestList[restourant][workerName] = salaryWorker
  20.          }
  21.       }
  22.       let entries = Object.entries(bestList).map(element => {
  23.          let name = element[0]
  24.          let workersLine = Object.values(element[1]).map(y => {
  25.             totalSalary += Number(y)
  26.          });
  27.          avgSalarary = totalSalary / workersLine.length
  28.          if (avgSalarary > currSalary) {
  29.             currSalary = avgSalarary
  30.             bestWorker = name
  31.             totalSalary = 0
  32.          };
  33.       });
  34.       let bestRestourant = Object.entries(bestList[bestWorker]).sort((a, b) => b[1] - a[1]);
  35.  
  36.       let finalResult = bestRestourant.reduce((acc, x) => {
  37.          return acc += `Name: ${x[0]} With Salary: ${x[1]} `
  38.       }, "");
  39.  
  40.       document.querySelector("#bestRestaurant p").textContent = `Name: ${bestWorker} Average Salary: ${currSalary.toFixed(2)} Best Salary: ${(bestRestourant[0][1].toFixed(2))}`
  41.       document.querySelector('#workers p').textContent = finalResult
  42.    }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement