Advertisement
Guest User

Problem4-RollandGarros

a guest
Apr 5th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     var playerArray = {};
  4.     var result = [];
  5.  
  6.     for (var index in input) {
  7.         //split and parse players and score
  8.         var line = input[index].split(/(vs.)+/g);
  9.         var player1 = line[0].trim();
  10.         line = line[2].split(/(:)+/g);
  11.  
  12.         var player2 = line[0].trim();
  13.         var score = line[2].trim().split(/\s+/g);
  14.  
  15.         //replace whitespaces between player names with a single space
  16.         player1 = player1.replace(/\s+/g, ' ');
  17.         player2 = player2.replace(/\s+/g, ' ');
  18.  
  19.         //set up variables for counting won and lost games, sets and matches
  20.         var player1GamesWon = 0;
  21.         var player2GamesWon = 0;
  22.         var player1GamesLost = 0;
  23.         var player2GamesLost = 0;
  24.         var player1SetsWon = 0;
  25.         var player2SetsWon = 0;
  26.         var player1SetsLost = 0;
  27.         var player2SetsLost = 0;
  28.         var player1MatchesWon = 0;
  29.         var player2MatchesWon = 0;
  30.         var player1MatchesLost = 0;
  31.         var player2MatchesLost = 0;
  32.  
  33.         //calculate the wins and losses
  34.         for (var i in score) {
  35.             var setScore = score[i].split('-');
  36.             var player1Games = Number(setScore[0]);
  37.             var player2Games = Number(setScore[1]);
  38.  
  39.             //calculate games each player has won
  40.             player1GamesWon += player1Games;
  41.             player2GamesWon += player2Games;
  42.  
  43.             //calculate games each player has lost
  44.             player1GamesLost += player2Games;
  45.             player2GamesLost += player1Games;
  46.  
  47.             //calculate won and lost sets for each player
  48.             if (player1Games > player2Games) {
  49.                 player1SetsWon += 1;
  50.                 player2SetsLost += 1;
  51.             } else {
  52.                 player2SetsWon += 1;
  53.                 player1SetsLost += 1;
  54.             }
  55.         }
  56.  
  57.         //calculate the match wins and losses
  58.         if (player1SetsWon > player2SetsWon) {
  59.             player1MatchesWon += 1;
  60.             player2MatchesLost += 1;
  61.         } else {
  62.             player2MatchesWon += 1;
  63.             player1MatchesLost += 1;
  64.         }
  65.  
  66.         //create new objects for the players
  67.         //if they don't exist
  68.         if(!playerArray[player1]) {
  69.             playerArray[player1] = {
  70.                 'name': player1,
  71.                 'matchesWon': 0,
  72.                 'matchesLost': 0,
  73.                 'setsWon': 0,
  74.                 'setsLost': 0,
  75.                 'gamesWon': 0,
  76.                 'gamesLost': 0
  77.             };
  78.         }
  79.         if(!playerArray[player2]) {
  80.             playerArray[player2] = {
  81.                 'name': player2,
  82.                 'matchesWon': 0,
  83.                 'matchesLost': 0,
  84.                 'setsWon': 0,
  85.                 'setsLost': 0,
  86.                 'gamesWon': 0,
  87.                 'gamesLost': 0
  88.             };
  89.         }
  90.  
  91.         //add the data for won and lost games, sets and matches
  92.         //to the object of player1
  93.         playerArray[player1].matchesWon += player1MatchesWon;
  94.         playerArray[player1].matchesLost += player1MatchesLost;
  95.         playerArray[player1].setsWon += player1SetsWon;
  96.         playerArray[player1].setsLost += player1SetsLost;
  97.         playerArray[player1].gamesWon += player1GamesWon;
  98.         playerArray[player1].gamesLost += player1GamesLost;
  99.  
  100.         //and player2
  101.         playerArray[player2].matchesWon += player2MatchesWon;
  102.         playerArray[player2].matchesLost += player2MatchesLost;
  103.         playerArray[player2].setsWon += player2SetsWon;
  104.         playerArray[player2].setsLost += player2SetsLost;
  105.         playerArray[player2].gamesWon += player2GamesWon;
  106.         playerArray[player2].gamesLost += player2GamesLost;
  107.     }
  108.  
  109.     //push the player objects in a new array
  110.     //we will sort this array
  111.     for (var key in playerArray) {
  112.         result.push(playerArray[key]);
  113.     }
  114.  
  115.     //sort function
  116.     function compare(player1, player2) {
  117.         //compare won matches
  118.         if (player1.matchesWon > player2.matchesWon) {
  119.             return -1;
  120.         } else if (player1.matchesWon < player2.matchesWon) {
  121.             return 1;
  122.         //if both players have an equal number of wins
  123.         } else if (player1.matchesWon == player2.matchesWon) {
  124.             //compare won sets
  125.             if (player1.setsWon > player2.setsWon) {
  126.                 return -1;
  127.             } else if (player1.setsWon < player2.setsWon) {
  128.                 return 1;
  129.             //if both players have won an equal number of sets
  130.             } else if (player1.setsWon == player2.setsWon) {
  131.                 //compare won games
  132.                 if (player1.gamesWon > player2.gamesWon) {
  133.                     return -1;
  134.                 } else if (player1.gamesWon < player2.gamesWon) {
  135.                     return 1;
  136.                 //if the games are also equal
  137.                 } else if (player1.gamesWon == player2.gamesWon) {
  138.                     //sort the players by their name
  139.                     return player1.name.localeCompare(player2.name);
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     //sort the result array with the function
  146.     result.sort(compare);
  147.  
  148.     //print the result
  149.     console.log(JSON.stringify(result));
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement