Advertisement
dimipan80

Exams - Prices Trends

Nov 19th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a list of prices. Your task is to print them in a HTML table: the first column holds a price;
  2. the second column holds a trend. The trend is either fixed (no change) or moving up or moving down.
  3. Fixed is the trend of the first price and when the previous price is the same as the current price
  4. (after rounding). Moving up is when the current price is greater than the previous price (after rounding).
  5. Moving down is when the current price is less than the previous price (after rounding).
  6. All numbers are rounded to 2 digits after the decimal point. See the examples below for better understanding.
  7. The input is passed as array of strings holding the input numbers. The table has a fixed header defining
  8. 2 columns: Price and Trend. The prices column should hold the price, rounded to 2 decimal places.
  9. The trend is calculated after rounding (with 2 decimal places) and can be "fixed", "up" or "down". */
  10.  
  11. "use strict";
  12.  
  13. function printTableOfPrices(arr) {
  14.         console.log('<table>');
  15.     console.log('<tr><th>Price</th><th>Trend</th></tr>');
  16.  
  17.     function comparePrices(price1, price2) {
  18.         return (Number(price1) > Number(price2)) ? '"down.png"' : (Number(price1) < Number(price2)) ?
  19.             '"up.png"' : '"fixed.png"';
  20.     }
  21.  
  22.     var previousPrice = parseFloat(args[0]).toFixed(2);
  23.     for (var i = 0; i < args.length; i += 1) {
  24.         var price = parseFloat(args[i]).toFixed(2);
  25.         var source = comparePrices(previousPrice, price);
  26.  
  27.         console.log('<tr><td>' + price + '</td><td><img src=' + source + '/></td></td>');
  28.         previousPrice = price;
  29.     }
  30.  
  31.     console.log('</table>');
  32. }
  33.  
  34. printTableOfPrices([ '50', '60' ]);
  35. printTableOfPrices([ '36.333', '36.5', '37.019', '35.4', '35', '35.001', '36.225' ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement