Advertisement
nikolayneykov

Untitled

Apr 9th, 2019
109
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.     if (!countries[country].hasOwnProperty(contestant)) {
  12.       countries[country][contestant] = 0
  13.     }
  14.  
  15.     countries[country][contestant] += Number(points)
  16.   })
  17.  
  18.   Object.entries(countries)
  19.     .sort((a, b) => {
  20.       let aSum = Object.values(b[1]).reduce((n1, n2) => n1 + n2, 0)
  21.       let bSum = Object.values(a[1]).reduce((n1, n2) => n1 + n2, 0)
  22.       return aSum - bSum
  23.     })
  24.     .forEach(country => {
  25.       let [countryName, totalPoints] = [
  26.         country[0],
  27.         Object.values(country[1]).reduce((a, b) => a + b, 0)
  28.       ]
  29.       console.log(`${countryName}: ${totalPoints}`)
  30.  
  31.       Object.entries(country[1]).forEach(contestant => {
  32.         console.log(`-- ${contestant[0]} -> ${contestant[1]}`)
  33.       })
  34.     })
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement