nikolayneykov

Untitled

Apr 19th, 2019
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (params) {
  2.   params.pop()
  3.   let countries = {}
  4.  
  5.   params.forEach(param => {
  6.     let [country, contestant, points] = param.split(' -> ')
  7.  
  8.     if (!countries.hasOwnProperty(country)) {
  9.       countries[country] = {}
  10.     }
  11.  
  12.     if (!countries[country].hasOwnProperty(contestant)) {
  13.       countries[country][contestant] = 0
  14.     }
  15.  
  16.     countries[country][contestant] += Number(points)
  17.   })
  18.  
  19.   Object.entries(countries)
  20.     .sort((a, b) => {
  21.       let aSum = Object.values(b[1]).reduce((n1, n2) => n1 + n2, 0)
  22.       let bSum = Object.values(a[1]).reduce((n1, n2) => n1 + n2, 0)
  23.       return aSum - bSum
  24.     })
  25.     .forEach(country => {
  26.       let [countryName, totalPoints] = [
  27.         country[0],
  28.         Object.values(country[1]).reduce((a, b) => a + b, 0)
  29.       ]
  30.       console.log(`${countryName}: ${totalPoints}`)
  31.  
  32.       Object.entries(country[1]).forEach(contestant => {
  33.         console.log(`-- ${contestant[0]} -> ${contestant[1]}`)
  34.       })
  35.     })
  36. }
Add Comment
Please, Sign In to add comment