Advertisement
dimipan80

Exams - Vladko's Notebook

Dec 22nd, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a list of colored sheets given as text table with the following columns:
  2.      Option 01 – [color of the sheet]|[win/loss]|[opponent name]
  3.      Option 02 – [color of the sheet]|[name]|[player name]
  4.      Option 03 – [color of the sheet]|[age]|[player age]
  5.  The different columns will always be separated only by 'I' (there won't be any whitespaces).
  6.  The rank of each player is calculated by the formula rank = (wins+1) / (losses+1).
  7.  If a certain color sheet has no information about the name or the age of the player, you should not print it
  8.  in the output. If there is no information about opponents, you must print '[]'.
  9.  There might be many opponents with the same names. You will know that they are different ones by the color
  10.  of the sheets. Write a JavaScript function to aggregate the results and print them as JSON string.
  11.  Print at the console a JSON string that holds the age, the name, an array with the opponents (in alphabetical order)
  12.  and rank of the player. The rank of the players should be rounded to 2 digits after the decimal point. */
  13.  
  14. "use strict";
  15.  
  16. function solve(args) {
  17.     var notebook = {};
  18.  
  19.     function putRowInfoToObject(object, dataLine) {
  20.         var color = dataLine[0];
  21.         if (!(color in object)) {
  22.             object[color] = {'age': '', 'name': '', 'opponents': [], 'rank': '', 'win': 1, 'loss': 1};
  23.         }
  24.  
  25.         if (dataLine[1] == 'age') {
  26.             object[color].age = dataLine[2];
  27.         } else if (dataLine[1] == 'name') {
  28.             object[color].name = dataLine[2];
  29.         } else {
  30.             object[color].opponents.push(dataLine[2]);
  31.             (dataLine[1] == 'win') ?  object[color].win += 1 : object[color].loss += 1;
  32.         }
  33.     }
  34.  
  35.     for (var i = 0; i < args.length; i += 1) {
  36.         var line = args[i].split('|');
  37.         putRowInfoToObject(notebook, line);
  38.     }
  39.  
  40.     notebook = aggregateObject(notebook);
  41.     console.log(JSON.stringify(notebook));
  42.  
  43.     function aggregateObject(object) {
  44.         for (var key in object) {
  45.             if (object.hasOwnProperty(key)) {
  46.                 if (object[key].age && object[key].name) {
  47.                     object[key].opponents.sort();
  48.                     object[key].rank =
  49.                         (object[key].win / object[key].loss).toFixed(2);
  50.                     delete object[key].win;
  51.                     delete object[key].loss;
  52.                 } else {
  53.                     delete object[key];
  54.                 }
  55.             }
  56.         }
  57.  
  58.         var newObject = {};
  59.         var sortedColors = Object.keys(object).sort();
  60.         for (var i = 0; i < sortedColors.length; i += 1) {
  61.             if (object.hasOwnProperty(sortedColors[i])) {
  62.                 newObject[sortedColors[i]] = object[sortedColors[i]];
  63.             }
  64.         }
  65.  
  66.         return newObject;
  67.     }
  68. }
  69.  
  70. solve([
  71.     'purple|age|99',
  72.     'red|age|44',
  73.     'blue|win|pesho',
  74.     'blue|win|mariya',
  75.     'purple|loss|Kiko',
  76.     'purple|loss|Kiko',
  77.     'purple|loss|Kiko',
  78.     'purple|loss|Yana',
  79.     'purple|loss|Yana',
  80.     'purple|loss|Manov',
  81.     'purple|loss|Manov',
  82.     'red|name|gosho',
  83.     'blue|win|Vladko',
  84.     'purple|loss|Yana',
  85.     'purple|name|VladoKaramfilov',
  86.     'blue|age|21',
  87.     'blue|loss|Pesho'
  88. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement