Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- document.querySelector('#btnSend').addEventListener('click', onClick);
- function onClick() {
- let input = JSON.parse(document.querySelector("textarea").value);
- let bestRestaurantOutput = document.querySelector('#bestRestaurant p');
- let bestWorkersOutput = document.querySelector('#workers p');
- let restInfo = {};
- let restaurant = '';
- let avg = 0;
- let currentName = '';
- for (let i = 0; i < input.length; i++) {
- restaurant = input[i].split("-")[0];
- let workersInfo = input[i].split(' - ')[1].split(", ");
- if (!restInfo[restaurant]) {
- restInfo[restaurant] = {};
- }
- for (let j = 0; j < workersInfo.length; j++) {
- let worker = workersInfo[j].split(" ")[0];
- let salary = Number(workersInfo[j].split(" ")[1]);
- restInfo[restaurant][worker] = salary;
- }
- let arr2 = Object.values(restInfo[restaurant])
- let currentAvg = 0;
- for (const el of arr2) {
- currentAvg += el;
- }
- currentAvg = (currentAvg / arr2.length).toFixed(2)
- if (avg < currentAvg) {
- avg = currentAvg
- currentName = restaurant;
- }
- }
- let bestSalary = Math.max(...(Object.values(restInfo[currentName]))).toFixed(2)
- Object.entries(restInfo[currentName])
- .sort((a, b) => {
- return b[1] - a[1];
- })
- .forEach((el) => {
- bestWorkersOutput.innerHTML += `Name: ${el[0]} With Salary: ${el[1]} `
- })
- bestRestaurantOutput.innerHTML = `Name: ${currentName} Average Salary: ${avg} Best Salary: ${bestSalary}`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement