Advertisement
dimipan80

Exams - Soccer Results

Nov 17th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a sequence of soccer results in format "homeTeam / awayTeam: homeGoals-awayGoals".
  2.  Your task is to write a JavaScript function that prints at the console a JSON string that holds the teams,
  3.  and for each team goals scored, goals conceded and a list of teams that had a match with this team in
  4.  the same format like at the below examples. Print at the console a JSON string that holds the teams
  5.  (in alphabetical order) and for each team goals scored, goals conceded and a list of teams that had
  6.  a match with this team (in alphabetical order). Duplicated teams should be printed once only
  7.  (all strings are case-sensitive). */
  8.  
  9. "use strict";
  10.  
  11. function sorterSoccerResults(args) {
  12.     var soccerMap = {};
  13.     var pattern = /([^/]+)\/([^:]+):([^-]+)-\s*(\d{1,2})/g;
  14.     for (var i = 0; i < args.length; i += 1) {
  15.         var matcher, homeTeam, awayTeam, homeGoals, awayGoals;
  16.         while (matcher = pattern.exec(args[i])) {
  17.             homeTeam = matcher[1].trim();
  18.             awayTeam = matcher[2].trim();
  19.             homeGoals = Number(matcher[3]);
  20.             awayGoals = Number(matcher[4]);
  21.         }
  22.  
  23.         addingInfoToObject(soccerMap, homeTeam, awayTeam, homeGoals, awayGoals);
  24.         addingInfoToObject(soccerMap, awayTeam, homeTeam, awayGoals, homeGoals);
  25.     }
  26.  
  27.     function addingInfoToObject(object, homeTeam, awayTeam, homeGoals, awayGoals) {
  28.         if (!(object.hasOwnProperty(homeTeam))) {
  29.             object[homeTeam] = { 'goalsScored': 0, 'goalsConceded': 0, 'matchesPlayedWith': [] };
  30.         }
  31.  
  32.         object[homeTeam].goalsScored += homeGoals;
  33.         object[homeTeam].goalsConceded += awayGoals;
  34.  
  35.         if (object[homeTeam].matchesPlayedWith.indexOf(awayTeam) < 0) {
  36.             object[homeTeam].matchesPlayedWith.push(awayTeam);
  37.         }
  38.     }
  39.  
  40.     function sortInfoInObject(object) {
  41.         var sortedHomeTeams = Object.keys(object).sort();
  42.         var sortedObject = {};
  43.         for (var i = 0; i < sortedHomeTeams.length; i += 1) {
  44.             if (object.hasOwnProperty(sortedHomeTeams[i])) {
  45.                 object[sortedHomeTeams[i]].matchesPlayedWith.sort();
  46.                 sortedObject[sortedHomeTeams[i]] = object[sortedHomeTeams[i]];
  47.             }
  48.         }
  49.  
  50.         return sortedObject;
  51.     }
  52.  
  53.     soccerMap = sortInfoInObject(soccerMap);
  54.     console.log(JSON.stringify(soccerMap));
  55. }
  56.  
  57. sorterSoccerResults([
  58.     "Germany / Argentina: 1-0",
  59.     "Brazil / Netherlands: 0-3",
  60.     "Netherlands / Argentina: 0-0",
  61.     "Brazil / Germany: 1-7",
  62.     "Argentina / Belgium: 1-0",
  63.     "Netherlands / Costa Rica: 0-0",
  64.     "France / Germany: 0-1",
  65.     "Brazil / Colombia: 2-1" ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement