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