Advertisement
Mitfreex

grades, avg

Jan 17th, 2022
744
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let list = {};
  4.  
  5.     for (let current of input) {
  6.  
  7.         let [name, ...grades] = current.split(' ');
  8.         grades = grades.map(Number)
  9.  
  10.  
  11.         if (list.hasOwnProperty(name)) {
  12.  
  13.             let existingGrades = list[name];
  14.             list[name] = [...existingGrades, ...grades];
  15.         } else {
  16.             list[name] = grades;
  17.         }
  18.     }
  19.  
  20.     function avg(x) {
  21.  
  22.         let sum = 0;
  23.         for (let current of x) {
  24.             sum += current;
  25.         }
  26.         return sum / x.length
  27.     }
  28.  
  29.     let sorted = Object.entries(list)
  30.         .sort((a, b) => avg(a[1]) - avg(b[1]));
  31.  
  32.  
  33.     for (let [name, grades] of sorted) {
  34.  
  35.         console.log(name + ': ' + grades.join(', '));
  36.     }
  37.  
  38. }
  39.  
  40.  
  41. solve(['Lilly 4 6 6 5',
  42.     'Tim 5 6',
  43.     'Tammy 2 4 3',
  44.     'Tim 6 6'
  45. ])
  46.  
  47. /*
  48.  
  49. */
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement