Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) {
- let playersObj = {};
- let dataObj = {};
- for (let string of inputArr) {
- if (string === 'Season end') {
- break;
- }
- if (!string.includes('vs')) {
- let [player, position, skill] = string.split(' -> ');
- skill = +skill;
- if (!playersObj.hasOwnProperty(player)) {
- playersObj[player] = {};
- dataObj = playersObj[player];
- dataObj[position] = skill;
- } else {
- if (!playersObj[player].hasOwnProperty(position)) {
- let currentDataObj = playersObj[player];
- currentDataObj[position] = skill;
- } else {
- let currentDataObj = playersObj[player];
- if (currentDataObj[position] < skill) {
- currentDataObj[position] = skill;
- }
- }
- }
- } else {
- let [firstPlayer, secondPlayer] = string.split(' vs ');
- if (playersObj.hasOwnProperty(firstPlayer) && playersObj.hasOwnProperty(secondPlayer)) {
- let firstPlayerPositions = Object.keys(playersObj[firstPlayer]);
- let firstPlayerSkills = Object.values(playersObj[firstPlayer]);
- let firstPlayerTotalSkill = firstPlayerSkills.reduce((a, b) => a + b, 0);
- let secondPlayerPositions = Object.keys(playersObj[secondPlayer]);
- let secondPlayerSkills = Object.values(playersObj[secondPlayer]);
- let secondPlayerTotalSkill = secondPlayerSkills.reduce((a, b) => a + b, 0);
- for (let currentPosition of firstPlayerPositions) {
- if (secondPlayerPositions.includes(currentPosition)) {
- if (firstPlayerTotalSkill > secondPlayerTotalSkill) {
- delete playersObj[secondPlayer];
- } else if (firstPlayerTotalSkill < secondPlayerTotalSkill) {
- delete playersObj[firstPlayer];
- }
- }
- }
- }
- }
- }
- let playersArr = Object.keys(playersObj);
- for (let player of playersArr) {
- let totalSkill = Object.values(playersObj[player]).reduce((a, b) => a + b, 0);
- let currentDataObj = playersObj[player];
- currentDataObj['totalSkill'] = totalSkill;
- }
- let playersResult = Object.entries(playersObj).sort(sorter);
- for (let player of playersResult) {
- let playerPositions = Object.entries(playersObj[player[0]]).sort(skillSorter);
- console.log(`${player[0]}: ${player[1].totalSkill} skill`);
- for (let position of playerPositions) {
- if (position[0] !== 'totalSkill') {
- console.log(`- ${position[0]} <::> ${position[1]}`);
- }
- }
- }
- function sorter(a, b) {
- let [aKey, aValue] = a;
- let [bKey, bValue] = b;
- let firstCriteria = bValue.totalSkill - aValue.totalSkill;
- if (firstCriteria === 0) {
- return aKey.localeCompare(bKey);
- }
- return firstCriteria
- }
- function skillSorter(a, b) {
- let firstCriteria = b[1] - a[1];
- if (firstCriteria === 0) {
- return a[0].localeCompare(b[0]);
- }
- return firstCriteria;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment