Advertisement
dimipan80

Exams - Biggest Table Row

Nov 20th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a HTML table of 4 columns: Town, Store1, Store2 and Store3. It consists of sequence
  2.  of text lines: the "<table>" tag, the header row, several data rows, and "</table>" tag (see the examples below).
  3.  The Store1, Store2, and Store3 columns hold either numbers or "-" (which means "no data"). Your task is
  4.  to write a JavaScript function which parses the table data rows and finds the row with a maximal sum of
  5.  its values. The input is passed as array of strings holding the table lines. Print at the console a single line,
  6.  holding the data row values with a maximal sum in format: "sum = value1 + values2 + …". Print the values
  7.  exactly as they were found in the input (no rounding, no reformatting). If all rows contain no data,
  8.  print "no data". If two rows have the same maximal sum, print the first of them.*/
  9.  
  10. "use strict";
  11.  
  12. function findTheBiggestTableRowSum(arr) {
  13.         var rowsArr = [];
  14.     var pattern = /^<tr><td>[^<]+<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><\/tr>$/g;
  15.  
  16.     for (var i = 2; i + 1 < args.length; i += 1) {
  17.         var town = { 'sum': 0, 'stores': []};
  18.         var matcher;
  19.         while (matcher = pattern.exec(args[i])) {
  20.             town.sum = calculateSumOfTown(matcher);
  21.             if (Number(matcher[1]) == matcher[1]) town.stores.push(matcher[1]);
  22.             if (Number(matcher[2]) == matcher[2]) town.stores.push(matcher[2]);
  23.             if (Number(matcher[3]) == matcher[3]) town.stores.push(matcher[3]);
  24.         }
  25.  
  26.         if (town.stores.length) {
  27.             rowsArr.push(town);
  28.         }
  29.     }
  30.  
  31.     function calculateSumOfTown(arr) {
  32.         var sum = 0;
  33.         for (var i = 1; i < 4; i += 1) {
  34.             if (Number(arr[i]) == arr[i]) {
  35.                 sum += Number(arr[i]);
  36.             }
  37.         }
  38.  
  39.         return sum;
  40.     }
  41.  
  42.     if (!rowsArr.length) {
  43.         console.log('no data');
  44.     } else {
  45.         var indexBiggest = 0;
  46.         if (rowsArr.length > 1) {
  47.             var maxSum = -3000001;
  48.             for (i = 0; i < rowsArr.length; i += 1) {
  49.                 if (rowsArr[i].sum > maxSum) {
  50.                     maxSum = rowsArr[i].sum;
  51.                     indexBiggest = i;
  52.                 }
  53.             }
  54.         }
  55.  
  56.         var result = '';
  57.         for (var prop in rowsArr[indexBiggest]) {
  58.             if (rowsArr[indexBiggest].hasOwnProperty(prop)) {
  59.                 if (prop == 'sum') {
  60.                     result += rowsArr[indexBiggest][prop] + ' = ';
  61.                 } else {
  62.                     result += rowsArr[indexBiggest][prop].join(' + ');
  63.                 }
  64.             }
  65.         }
  66.         console.log(result);
  67. }
  68.  
  69. findTheBiggestTableRowSum(['<table>',
  70.     '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
  71.     '<tr><td>Sofia</td><td>26.2</td><td>8.20</td><td>-</td></tr>',
  72.     '<tr><td>Varna</td><td>11.2</td><td>18.00</td><td>36.10</td></tr>',
  73.     '<tr><td>Plovdiv</td><td>17.2</td><td>12.3</td><td>6.4</td></tr>',
  74.     '<tr><td>Bourgas</td><td>-</td><td>24.3</td><td>-</td></tr>',
  75.     '</table>'
  76. ]);
  77.  
  78. findTheBiggestTableRowSum(['<table>',
  79.     '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
  80.     '<tr><td>Sofia</td><td>-</td><td>-</td><td>-</td></tr>',
  81.     '</table>'
  82. ]);
  83.  
  84. findTheBiggestTableRowSum(['<table>',
  85.     '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
  86.     '<tr><td>Sofia</td><td>12850</td><td>-560</td><td>20833</td></tr>',
  87.     '<tr><td>Rousse</td><td>-</td><td>50000.0</td><td>-</td></tr>',
  88.     '<tr><td>Bourgas</td><td>25000</td><td>25000</td><td>-</td></tr>',
  89.     '</table>'
  90. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement