Advertisement
dimipan80

Exams - The Teteven Trip

Jan 11th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You will receive as input data an array of n elements. Each element will consist of a string in the format:
  2.  * "[car model] [fuel type] [route number] [luggage weight]", separated by exactly 1 space.
  3.  * The base fuel consumption is 10L per 100km. There are 3 fuel types - 'gas', 'petrol' and 'diesel'.
  4.  * Each type has a correction coefficient c: for gas c = 1.2, for petrol c = 1, for diesel c = 0.8.
  5.  * For every kg of luggage you need to add 0.01L extra fuel consumption.
  6.  * There are 2 routes to Teteven - route '1' and route '2'. Route '1' is long 110km (100km normal road
  7.  * and 10km snowy road). Route '2' is long 95km (65km normal road and 30km snowy road).
  8.  * The total fuel consumption is calculated by multiplying the base fuel consumption and the total route distance.
  9.  * Then you should add the route's snow distance multiplied by 30% of the base fuel consumption.
  10.  * The end you should round the fuel liters to an integer value!
  11.  * The output consist of n lines. Each line should hold the car model, the fuel type, the route number, and
  12.  * the quantity of fuel needed in liters (all space-separated). The quantity of fuel is rounded to
  13.  * an integer number. */
  14.  
  15. "use strict";
  16.  
  17. function solve(args) {
  18.     args.forEach(function (line) {
  19.         var lineArr = line.split(/\s/);
  20.         var model = lineArr[0];
  21.         var fuelType = lineArr[1];
  22.         var routeNum = lineArr[2];
  23.         var luggageWeight = parseFloat(lineArr[3]);
  24.        
  25.         var coefficient = 1;
  26.         if (fuelType == 'gas') {
  27.             coefficient = 1.2;
  28.         } else if (fuelType == 'diesel') {
  29.             coefficient = 0.8;
  30.         }
  31.        
  32.         var fuelConsumption = (10 * coefficient + 0.01 * luggageWeight) / 100;
  33.         var extraSnow = 0.3 * fuelConsumption;
  34.        
  35.         if (routeNum == '1') {
  36.             fuelConsumption = (110 * fuelConsumption) + (10 * extraSnow);
  37.         } else {
  38.             fuelConsumption = (95 * fuelConsumption) + (30 * extraSnow);
  39.         }
  40.        
  41.         console.log("%s %s %d %d", model, fuelType, routeNum, Math.round(fuelConsumption));
  42.     });
  43. }
  44.  
  45. solve([
  46.     'BMW petrol 1 320.5',
  47.     'Golf petrol 2 150.75',
  48.     'Lada gas 1 202',
  49.     'Mercedes diesel 2 312.54'
  50. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement