Advertisement
enevlogiev

Untitled

Apr 2nd, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function solve(input) {
  4.     var command = input.pop(),
  5.         touristsData= {};
  6.  
  7.     for (var line in input) {
  8.         input[line] = input[line].split(/\.*\*\.*/g);
  9.         var name = input[line][0];
  10.         if (!touristsData[name]) {
  11.             touristsData[name] = {};
  12.         }
  13.     }
  14.  
  15.     if (command === 'luggage name') {
  16.         input.sort(function(a, b) {
  17.             return a[1] > b[1];
  18.         });
  19.     } else if (command === 'weight') {
  20.         input.sort(function (a, b) {
  21.             return parseFloat(a[5]) - parseFloat(b[5]);
  22.         });
  23.     }
  24.  
  25.  
  26.     input.forEach(function(line) {
  27.         var name = line[0],
  28.             luggageName = line[1],
  29.             isFood = line[2] === 'true',
  30.             isDrink = line[3] === 'true',
  31.             isFragile = line[4] === 'true',
  32.             weight = parseFloat(line[5]),
  33.             transferredWith = line[6],
  34.             luggageType = 'other';
  35.         if (isDrink) {
  36.             luggageType = 'drink';
  37.         } else if (isFood) {
  38.             luggageType = 'food';
  39.         }
  40.         touristsData[name][luggageName] = {
  41.             kg: weight,
  42.             fragile: isFragile,
  43.             type: luggageType,
  44.             transferredWith: transferredWith
  45.         }
  46.     });
  47.  
  48.     console.log(JSON.stringify(touristsData));
  49. }
  50.  
  51.  
  52.  
  53.  
  54. solve ([
  55. 'Yana Slavcheva...*..clothes..*false*false......*...false..*..2.2..*.backpack',
  56. 'Kiko..*...socks..*false*false......*...false..*..0.2..*.backpack',
  57. 'Kiko....*...banana..*true*false......*...false..*..3.2..*.backpack',
  58. 'Kiko......*...sticks..*false.....*false......*...false..*..1.6..*.ATV',
  59. 'Kiko*...glasses..*false*...false......*...true..*..3..*.ATV',
  60. 'Manov..*...socks..*false*false....*...false..*0.3..*.ATV',
  61. 'luggage name']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement