Advertisement
Guest User

Untitled

a guest
Jan 26th, 2021
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. function solve() {
  2. document.querySelector('#btnSend').addEventListener('click', onClick);
  3.  
  4. function onClick() {
  5. let arr = JSON.parse(document.querySelector('#inputs textarea').value);
  6. let objWinner = findBestRestaurant(arr);
  7. document.getElementById('bestRestaurant').innerHTML = getMsgRest(objWinner);
  8. document.getElementById('workers').textContent = getMsgEmp(objWinner.workers);
  9. }
  10.  
  11. function getMsgRest(objWinner) {
  12. return `Name: ${objWinner.name} Average Salary: ${objWinner['avg']} Best Salary: ${objWinner['maxSalary']}`
  13. }
  14.  
  15. function getMsgEmp(workers) {
  16. let arr = [];
  17. for (let i = 0; i < workers.length; i += 2) {
  18. arr.push(`Name: ${workers[i]} With Salary: ${workers[i + 1]}`);
  19. }
  20. return arr.join(' ');
  21. }
  22.  
  23. function findBestRestaurant(arr) {
  24.  
  25. let result = arr.slice(0).reduce((acc, e) => {
  26. let [restaurant, ...workers] = e.split(' - ');
  27.  
  28. if (acc[restaurant] === undefined) {
  29. acc[restaurant] = [];
  30. }
  31.  
  32. workers[0].split(', ').forEach(w => {
  33. let [name, salary] = w.split(' ')
  34. salary = Number(salary);
  35. acc[restaurant].push(name)
  36. acc[restaurant].push(Number(salary))
  37.  
  38. })
  39. return acc;
  40. }, {});
  41.  
  42. let addAdditionalParams = [];
  43. Object.entries(result).reduce((acc, e) => {
  44. let totalSum = e[1].filter(e => !isNaN(e)).reduce((acc, e) => {
  45. acc = acc + e;
  46. return acc;
  47. }, 0);
  48.  
  49. const topSal = e[1].filter(e => !isNaN(e)).sort((a, b) => b - a);
  50. let currMaxSalary = topSal[0].toFixed(2);
  51.  
  52. const avg = (totalSum / (e[1].length / 2)).toFixed(2);
  53. acc = {name: e[0], avg: avg, maxSalary: currMaxSalary, workers: e[1]}
  54. addAdditionalParams.push(acc);
  55. return acc;
  56. }, {});
  57.  
  58. let obj = {};
  59. let maxSalary = Number.MIN_SAFE_INTEGER;
  60. addAdditionalParams.forEach(e => {
  61. let avg = e.avg;
  62. if (avg > maxSalary) {
  63. maxSalary = avg;
  64. obj = e;
  65. }
  66. })
  67. return obj;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement