Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- var playerArray = {};
- var result = [];
- for (var index in input) {
- //split and parse players and score
- var line = input[index].split(/(vs.)+/g);
- var player1 = line[0].trim();
- line = line[2].split(/(:)+/g);
- var player2 = line[0].trim();
- var score = line[2].trim().split(/\s+/g);
- //replace whitespaces between player names with a single space
- player1 = player1.replace(/\s+/g, ' ');
- player2 = player2.replace(/\s+/g, ' ');
- //set up variables for counting won and lost games, sets and matches
- var player1GamesWon = 0;
- var player2GamesWon = 0;
- var player1GamesLost = 0;
- var player2GamesLost = 0;
- var player1SetsWon = 0;
- var player2SetsWon = 0;
- var player1SetsLost = 0;
- var player2SetsLost = 0;
- var player1MatchesWon = 0;
- var player2MatchesWon = 0;
- var player1MatchesLost = 0;
- var player2MatchesLost = 0;
- //calculate the wins and losses
- for (var i in score) {
- var setScore = score[i].split('-');
- var player1Games = Number(setScore[0]);
- var player2Games = Number(setScore[1]);
- //calculate games each player has won
- player1GamesWon += player1Games;
- player2GamesWon += player2Games;
- //calculate games each player has lost
- player1GamesLost += player2Games;
- player2GamesLost += player1Games;
- //calculate won and lost sets for each player
- if (player1Games > player2Games) {
- player1SetsWon += 1;
- player2SetsLost += 1;
- } else {
- player2SetsWon += 1;
- player1SetsLost += 1;
- }
- }
- //calculate the match wins and losses
- if (player1SetsWon > player2SetsWon) {
- player1MatchesWon += 1;
- player2MatchesLost += 1;
- } else {
- player2MatchesWon += 1;
- player1MatchesLost += 1;
- }
- //create new objects for the players
- //if they don't exist
- if(!playerArray[player1]) {
- playerArray[player1] = {
- 'name': player1,
- 'matchesWon': 0,
- 'matchesLost': 0,
- 'setsWon': 0,
- 'setsLost': 0,
- 'gamesWon': 0,
- 'gamesLost': 0
- };
- }
- if(!playerArray[player2]) {
- playerArray[player2] = {
- 'name': player2,
- 'matchesWon': 0,
- 'matchesLost': 0,
- 'setsWon': 0,
- 'setsLost': 0,
- 'gamesWon': 0,
- 'gamesLost': 0
- };
- }
- //add the data for won and lost games, sets and matches
- //to the object of player1
- playerArray[player1].matchesWon += player1MatchesWon;
- playerArray[player1].matchesLost += player1MatchesLost;
- playerArray[player1].setsWon += player1SetsWon;
- playerArray[player1].setsLost += player1SetsLost;
- playerArray[player1].gamesWon += player1GamesWon;
- playerArray[player1].gamesLost += player1GamesLost;
- //and player2
- playerArray[player2].matchesWon += player2MatchesWon;
- playerArray[player2].matchesLost += player2MatchesLost;
- playerArray[player2].setsWon += player2SetsWon;
- playerArray[player2].setsLost += player2SetsLost;
- playerArray[player2].gamesWon += player2GamesWon;
- playerArray[player2].gamesLost += player2GamesLost;
- }
- //push the player objects in a new array
- //we will sort this array
- for (var key in playerArray) {
- result.push(playerArray[key]);
- }
- //sort function
- function compare(player1, player2) {
- //compare won matches
- if (player1.matchesWon > player2.matchesWon) {
- return -1;
- } else if (player1.matchesWon < player2.matchesWon) {
- return 1;
- //if both players have an equal number of wins
- } else if (player1.matchesWon == player2.matchesWon) {
- //compare won sets
- if (player1.setsWon > player2.setsWon) {
- return -1;
- } else if (player1.setsWon < player2.setsWon) {
- return 1;
- //if both players have won an equal number of sets
- } else if (player1.setsWon == player2.setsWon) {
- //compare won games
- if (player1.gamesWon > player2.gamesWon) {
- return -1;
- } else if (player1.gamesWon < player2.gamesWon) {
- return 1;
- //if the games are also equal
- } else if (player1.gamesWon == player2.gamesWon) {
- //sort the players by their name
- return player1.name.localeCompare(player2.name);
- }
- }
- }
- }
- //sort the result array with the function
- result.sort(compare);
- //print the result
- console.log(JSON.stringify(result));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement