Advertisement
Guest User

JavaScript Basics Sample Exam July 2014 - Logs Aggregator

a guest
Jul 23rd, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Solve(args) {
  2.     var n = parseInt(args[0]);
  3.     var map = {};
  4.  
  5.     for (var i = 0; i < n; i++) {
  6.         var string = args[i+1];
  7.         var arrProperties = string.split(' ');
  8.         if (map[arrProperties[1]] == undefined) {
  9.             map[arrProperties[1]] = [parseInt(arrProperties[2]), []];
  10.         }
  11.         else {
  12.             map[arrProperties[1]][0] = map[arrProperties[1]][0] + parseInt(arrProperties[2]);
  13.         }
  14.         if (map[arrProperties[1]][1].indexOf(arrProperties[0]) == -1) {
  15.             map[arrProperties[1]][1].push(arrProperties[0]);
  16.         }
  17.     }
  18.    
  19.     var mapKeys = Object.keys(map);
  20.     mapKeys.sort();
  21.     mapKeys.forEach(function (key) {
  22.         map[key][1].sort();
  23.         console.log("%s: %d [%s]", key, map[key][0], map[key][1].join(", "));
  24.     });
  25. }
  26.  
  27. Solve(["7",
  28. "192.168.0.11 peter 33",
  29. "10.10.17.33 alex 12",
  30. "10.10.17.35 peter 30",
  31. "10.10.17.34 peter 120",
  32. "10.10.17.34 peter 120",
  33. "212.50.118.81 alex 46",
  34. "212.50.118.81 alex 4"]
  35. );
  36.  
  37. Solve(["2",
  38. "84.238.140.178 nakov 25",
  39. "84.238.140.178 nakov 35"]
  40. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement