Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) {
- let countries = {};
- for (const line of inputArr) {
- let [currCountry, contestant, points] = line.split(' -> ');
- if (line === 'END') {
- break;
- }
- if (!countries.hasOwnProperty(currCountry)) {
- countries[currCountry] = {};
- countries[currCountry][contestant] = +points
- countries[currCountry]['totalPoints'] = +points;
- } else if (countries.hasOwnProperty(currCountry)) {
- if (countries[currCountry].hasOwnProperty(contestant)) {
- countries[currCountry][contestant] += +points;
- countries[currCountry]['totalPoints'] += +points;
- } else {
- countries[currCountry][contestant] = +points;
- countries[currCountry]['totalPoints'] += +points;
- }
- }
- }
- let countriesArr = [];
- for (const country in countries) {
- let entries = Object.entries(countries[country]);
- let newObj = {
- name: country
- };
- for (let i = 0; i < entries.length; i++) {
- if (entries[i][0] === 'totalPoints') {
- newObj['totalPoints'] = entries[i][1];
- } else {
- newObj[entries[i][0]] = entries[i][1];
- }
- }
- countriesArr.push(newObj);
- }
- countriesArr.sort((a, b) => b.totalPoints - a.totalPoints);
- countriesArr.forEach(el => {
- console.log(`${el.name}: ${el.totalPoints}`);
- let entries = Object.entries(el);
- for (let i = 0; i < entries.length; i++) {
- if (entries[i][0] === 'name' || entries[i][0] === 'totalPoints') {
- continue;
- } else {
- console.log(` -- ${entries[i][0]} -> ${entries[i][1]}`)
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement