Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You will receive as input data an array of n elements. Each element will consist of a string in the format:
- * "[car model] [fuel type] [route number] [luggage weight]", separated by exactly 1 space.
- * The base fuel consumption is 10L per 100km. There are 3 fuel types - 'gas', 'petrol' and 'diesel'.
- * Each type has a correction coefficient c: for gas c = 1.2, for petrol c = 1, for diesel c = 0.8.
- * For every kg of luggage you need to add 0.01L extra fuel consumption.
- * There are 2 routes to Teteven - route '1' and route '2'. Route '1' is long 110km (100km normal road
- * and 10km snowy road). Route '2' is long 95km (65km normal road and 30km snowy road).
- * The total fuel consumption is calculated by multiplying the base fuel consumption and the total route distance.
- * Then you should add the route's snow distance multiplied by 30% of the base fuel consumption.
- * The end you should round the fuel liters to an integer value!
- * The output consist of n lines. Each line should hold the car model, the fuel type, the route number, and
- * the quantity of fuel needed in liters (all space-separated). The quantity of fuel is rounded to
- * an integer number. */
- "use strict";
- function solve(args) {
- args.forEach(function (line) {
- var lineArr = line.split(/\s/);
- var model = lineArr[0];
- var fuelType = lineArr[1];
- var routeNum = lineArr[2];
- var luggageWeight = parseFloat(lineArr[3]);
- var coefficient = 1;
- if (fuelType == 'gas') {
- coefficient = 1.2;
- } else if (fuelType == 'diesel') {
- coefficient = 0.8;
- }
- var fuelConsumption = (10 * coefficient + 0.01 * luggageWeight) / 100;
- var extraSnow = 0.3 * fuelConsumption;
- if (routeNum == '1') {
- fuelConsumption = (110 * fuelConsumption) + (10 * extraSnow);
- } else {
- fuelConsumption = (95 * fuelConsumption) + (30 * extraSnow);
- }
- console.log("%s %s %d %d", model, fuelType, routeNum, Math.round(fuelConsumption));
- });
- }
- solve([
- 'BMW petrol 1 320.5',
- 'Golf petrol 2 150.75',
- 'Lada gas 1 202',
- 'Mercedes diesel 2 312.54'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement