Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a HTML table of 4 columns: Town, Store1, Store2 and Store3. It consists of sequence
- of text lines: the "<table>" tag, the header row, several data rows, and "</table>" tag (see the examples below).
- The Store1, Store2, and Store3 columns hold either numbers or "-" (which means "no data"). Your task is
- to write a JavaScript function which parses the table data rows and finds the row with a maximal sum of
- its values. The input is passed as array of strings holding the table lines. Print at the console a single line,
- holding the data row values with a maximal sum in format: "sum = value1 + values2 + …". Print the values
- exactly as they were found in the input (no rounding, no reformatting). If all rows contain no data,
- print "no data". If two rows have the same maximal sum, print the first of them.*/
- "use strict";
- function findTheBiggestTableRowSum(arr) {
- var rowsArr = [];
- var pattern = /^<tr><td>[^<]+<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><\/tr>$/g;
- for (var i = 2; i + 1 < args.length; i += 1) {
- var town = { 'sum': 0, 'stores': []};
- var matcher;
- while (matcher = pattern.exec(args[i])) {
- town.sum = calculateSumOfTown(matcher);
- if (Number(matcher[1]) == matcher[1]) town.stores.push(matcher[1]);
- if (Number(matcher[2]) == matcher[2]) town.stores.push(matcher[2]);
- if (Number(matcher[3]) == matcher[3]) town.stores.push(matcher[3]);
- }
- if (town.stores.length) {
- rowsArr.push(town);
- }
- }
- function calculateSumOfTown(arr) {
- var sum = 0;
- for (var i = 1; i < 4; i += 1) {
- if (Number(arr[i]) == arr[i]) {
- sum += Number(arr[i]);
- }
- }
- return sum;
- }
- if (!rowsArr.length) {
- console.log('no data');
- } else {
- var indexBiggest = 0;
- if (rowsArr.length > 1) {
- var maxSum = -3000001;
- for (i = 0; i < rowsArr.length; i += 1) {
- if (rowsArr[i].sum > maxSum) {
- maxSum = rowsArr[i].sum;
- indexBiggest = i;
- }
- }
- }
- var result = '';
- for (var prop in rowsArr[indexBiggest]) {
- if (rowsArr[indexBiggest].hasOwnProperty(prop)) {
- if (prop == 'sum') {
- result += rowsArr[indexBiggest][prop] + ' = ';
- } else {
- result += rowsArr[indexBiggest][prop].join(' + ');
- }
- }
- }
- console.log(result);
- }
- findTheBiggestTableRowSum(['<table>',
- '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
- '<tr><td>Sofia</td><td>26.2</td><td>8.20</td><td>-</td></tr>',
- '<tr><td>Varna</td><td>11.2</td><td>18.00</td><td>36.10</td></tr>',
- '<tr><td>Plovdiv</td><td>17.2</td><td>12.3</td><td>6.4</td></tr>',
- '<tr><td>Bourgas</td><td>-</td><td>24.3</td><td>-</td></tr>',
- '</table>'
- ]);
- findTheBiggestTableRowSum(['<table>',
- '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
- '<tr><td>Sofia</td><td>-</td><td>-</td><td>-</td></tr>',
- '</table>'
- ]);
- findTheBiggestTableRowSum(['<table>',
- '<tr><th>Town</th><th>Store1</th><th>Store2</th><th>Store3</th></tr>',
- '<tr><td>Sofia</td><td>12850</td><td>-560</td><td>20833</td></tr>',
- '<tr><td>Rousse</td><td>-</td><td>50000.0</td><td>-</td></tr>',
- '<tr><td>Bourgas</td><td>25000</td><td>25000</td><td>-</td></tr>',
- '</table>'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement