Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- var soccerTeams = {};
- var pattern = /([A-Za-z ]+)\s*\/\s*([A-Za-z ]+)\s*:\s*([0-9]+)\s*-\s*([0-9]+)/;
- for(var i = 0; i < input.length; i++) {
- var currentLine = pattern.exec(input[i]);
- var curHomeTeam = currentLine[1].trim();
- var curOpponent = currentLine[2].trim();
- var curHomeResult = Number(currentLine[3]);
- var curOpponentResult = Number(currentLine[4]);
- if(!soccerTeams[curHomeTeam]){
- soccerTeams[curHomeTeam] = {
- 'goalsScored': curHomeResult,
- 'goalsConceded': curOpponentResult,
- 'matchesPlayedWith': []
- };
- } else{
- soccerTeams[curHomeTeam].goalsScored += curHomeResult;
- soccerTeams[curHomeTeam].goalsConceded += curOpponentResult;
- }
- if(!soccerTeams[curOpponent]){
- soccerTeams[curOpponent] = {
- 'goalsScored': curOpponentResult,
- 'goalsConceded': curHomeResult,
- 'matchesPlayedWith': []
- };
- } else {
- soccerTeams[curOpponent].goalsScored += curOpponentResult;
- soccerTeams[curOpponent].goalsConceded += curHomeResult;
- }
- if(soccerTeams[curHomeTeam].matchesPlayedWith.indexOf(curOpponent) < 0){
- soccerTeams[curHomeTeam].matchesPlayedWith.push(curOpponent);
- }
- if(soccerTeams[curOpponent].matchesPlayedWith.indexOf(curHomeTeam) < 0){
- soccerTeams[curOpponent].matchesPlayedWith.push(curHomeTeam);
- }
- }
- soccerTeams = sortObjectProperties(soccerTeams);
- for(var team in soccerTeams){
- soccerTeams[team].matchesPlayedWith.sort();
- }
- console.log(JSON.stringify(soccerTeams));
- function sortObjectProperties(obj) {
- var keysSorted = Object.keys(obj).sort();
- var sortedObj = {};
- for (var i = 0; i < keysSorted.length; i++) {
- var key = keysSorted[i];
- sortedObj[key] = obj[key];
- }
- return sortedObj;
- }
- }
- solve(['Germany / Argentina: 1-0',
- 'Brazil / Netherlands: 0-3',
- 'Netherlands / Argentina: 0-0',
- 'Brazil / Germany: 1-7',
- 'Argentina / Belgium: 1-0',
- 'Netherlands / Costa Rica: 0-0',
- 'France / Germany: 0-1',
- 'Brazil / Colombia: 2-1'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement