Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) {
  2.     let countries = {};
  3.  
  4.     for (const line of inputArr) {
  5.         let [currCountry, contestant, points] = line.split(' -> ');
  6.        
  7.         if (line === 'END') {
  8.             break;
  9.         }
  10.  
  11.         if (!countries.hasOwnProperty(currCountry)) {
  12.             countries[currCountry] = {};
  13.             countries[currCountry][contestant] = +points
  14.             countries[currCountry]['totalPoints'] = +points;
  15.  
  16.         } else if (countries.hasOwnProperty(currCountry)) {
  17.  
  18.             if (countries[currCountry].hasOwnProperty(contestant)) {
  19.                 countries[currCountry][contestant] += +points;
  20.                 countries[currCountry]['totalPoints'] += +points;
  21.  
  22.             } else {
  23.                 countries[currCountry][contestant] = +points;
  24.                 countries[currCountry]['totalPoints'] += +points;
  25.             }
  26.         }
  27.     }
  28.    
  29.     let countriesArr = [];
  30.  
  31.     for (const country in countries) {
  32.         let entries = Object.entries(countries[country]);
  33.         let newObj = {
  34.             name: country
  35.         };
  36.         for (let i = 0; i < entries.length; i++) {
  37.             if (entries[i][0] === 'totalPoints') {
  38.                 newObj['totalPoints'] = entries[i][1];
  39.  
  40.             } else {
  41.                 newObj[entries[i][0]] = entries[i][1];
  42.             }
  43.         }
  44.         countriesArr.push(newObj);
  45.     }
  46.     countriesArr.sort((a, b) => b.totalPoints - a.totalPoints);
  47.     countriesArr.forEach(el => {
  48.         console.log(`${el.name}: ${el.totalPoints}`);
  49.         let entries = Object.entries(el);
  50.        
  51.         for (let i = 0; i < entries.length; i++) {
  52.  
  53.             if (entries[i][0] === 'name' || entries[i][0] === 'totalPoints') {
  54.                 continue;
  55.  
  56.             } else {
  57.                 console.log(` -- ${entries[i][0]} -> ${entries[i][1]}`)
  58.             }
  59.         }
  60.     });
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement