Advertisement
Guest User

[Exam Problems] JavaScript Basics Exam - 28 July 2014 - 04.

a guest
Mar 29th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var arr = ["Germany / Argentina: 1-0", "Brazil / Netherlands: 0-3", "Netherlands / Argentina: 0-0", "Brazil / Germany: 1-7",
  3.     "Argentina / Belgium: 1-0", "Netherlands / Costa Rica: 0-0", "France / Germany: 0-1", "Brazil / Colombia: 2-1" ]
  4. mess(arr)
  5.  
  6. function mess (input){
  7.     var result = {}
  8.     var arr = []
  9.     var obj = {}
  10.  
  11.     for (var i = 0; i < input.length; i++) {
  12.         var a = (input[i].trim().split(':'));  //console.log(a)
  13.         var key1 = ((a[0].trim().split('/'))[0]).trim();  //console.log(key1)
  14.         var key2 = ((a[0].trim().split('/'))[1]).trim();  //console.log(key2)
  15.         var value1 = Number((((a[1]).trim().split('-'))[0]).trim()); //console.log(value1)
  16.         var value2 = Number((((a[1]).trim().split('-'))[1]).trim()); //console.log(value2)
  17.         obj[key1] = value1
  18.         obj[key2] = value2; //console.log(obj)
  19.         arr.push(obj)
  20.         obj = {}
  21.     }
  22. //    console.log(arr)
  23.  
  24.     for (var i = 0; i < arr.length; i++) {
  25.         var countryfirst = Object.keys(arr[i])[0];
  26.         var countrysecond = Object.keys(arr[i])[1]//console.log(country)
  27.  
  28.         if (!result[countryfirst]){
  29.             result[countryfirst]= {goalsScored:0,goalsConceded:0,matchesPlayedWith:[] }
  30.         }
  31.         if (result[countryfirst].matchesPlayedWith.indexOf(countrysecond)==-1)
  32.         {
  33.             result[countryfirst].matchesPlayedWith.push(countrysecond)
  34.         }
  35. //        result[countryfirst].matchesPlayedWith.push(countrysecond)
  36.         result[countryfirst].goalsScored = result[countryfirst].goalsScored + arr[i][Object.keys(arr[i])[0]]
  37.         result[countryfirst].goalsConceded = result[countryfirst].goalsConceded + arr[i][Object.keys(arr[i])[1]]
  38.  
  39.  
  40.         if (!result[countrysecond]){
  41.             result[countrysecond]= {goalsScored:0,goalsConceded:0,matchesPlayedWith:[] }
  42.         }
  43.         if (result[countrysecond].matchesPlayedWith.indexOf(countryfirst)){
  44.             result[countrysecond].matchesPlayedWith.push(countryfirst)
  45.         }
  46.         result[countrysecond].goalsScored = result[countrysecond].goalsScored + arr[i][Object.keys(arr[i])[1]]
  47.         result[countrysecond].goalsConceded = result[countrysecond].goalsConceded + arr[i][Object.keys(arr[i])[0]]
  48.  
  49.  
  50.     }
  51.  
  52.     var keysarr = Object.keys(result)
  53.     keysarr.sort(); //console.log(keysarr)
  54.     var res = {}
  55.     for (var i = 0; i < keysarr.length; i++) {
  56.         res[keysarr[i]] = result[keysarr[i]]
  57.     }
  58.  
  59.  
  60. //    for (var i = 0; i < Object.keys(res).length; i++) {
  61. //        res[i].matchesPlayedWith.sort()
  62. //    }
  63.     for (var j in res){ res[j].matchesPlayedWith.sort()}
  64.     console.log(JSON.stringify(res))
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement