Emmanuel Akinfulubi

JavaScriptBasic

Calculate fuel consumption (L/100km) in JavaScript

Beginner · 3 min read

How thirsty is a car? There are two common ways to say it. Much of the world uses liters per 100 kilometers (L/100km): how much fuel it takes to drive 100 km. Other places use kilometers per liter: how far one liter takes you. Fleet trackers, car comparison sites and trip planners show one or both.

In this tutorial you will take a trip of 500 km that used 45 liters, work out both numbers, and see why "better" means a lower number for one and a higher number for the other. You need to know what a function is and how division works in JavaScript.

Two ways to describe one trip

Both measures come from the same two facts, the distance and the fuel. They just divide them the other way up.

  • Liters per 100 km is fuel divided by distance, then scaled to 100 km. It is a cost per distance, so lower is better.
  • Kilometers per liter is distance divided by fuel. It is a distance per cost, so higher is better.

If you sort a table of cars by the wrong one, the most economical car ends up at the bottom.

Name things after their unit

A variable called consumption could mean either measure. A variable called litresPer100 can only mean one. Naming by unit is the cheapest way to avoid mixing them up.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

68 Fuel consumption, two ways round

easy

A trip of 500km used 45 litres. Report the consumption both as litres per 100km and as kilometres per litre, and explain why the two rank cars in opposite directions.

Stuck? Three hints, smallest first
  1. 1. Litres per 100km is litres / km * 100.
  2. 2. Kilometres per litre is the other way up: km / litres.
  3. 3. One is a cost per distance, the other a distance per cost, so lower is better for one and higher for the other.

Step 1: liters per 100 km

Divide the fuel by the distance to get liters per kilometer, then multiply by 100:

Step 1

const litresPer100 = (litres, km) => (litres / km) * 100;
console.log(litresPer100(45, 500)); // 9

45 liters over 500 km is 0.09 liters per kilometer, which is 9 liters per 100 km.

Step 2: kilometers per liter

Divide the other way up:

Step 2

const kmPerLitre = (litres, km) => km / litres;
const round2 = (n) => Number(n.toFixed(2));
console.log(kmPerLitre(45, 500));         // 11.11111111111111
console.log(round2(kmPerLitre(45, 500))); // 11.11

round2 rounds to two decimal places. toFixed(2) gives text, and Number(...) turns it back into a number, so the result can still be compared and used in math.

Step 3: they are two views of the same fact

One is 100 divided by the other. If a car uses 9 liters per 100 km, then 100 km divided by 9 liters gives the kilometers per liter:

Step 3

const litresPer100 = (litres, km) => (litres / km) * 100;
const kmPerLitre = (litres, km) => km / litres;
const round2 = (n) => Number(n.toFixed(2));
console.log(round2(100 / litresPer100(45, 500))); // 11.11
console.log(round2(kmPerLitre(45, 500)));         // 11.11

Now compare a thirstier car that used 60 liters for the same 500 km:

JavaScript

const litresPer100 = (litres, km) => (litres / km) * 100;
const kmPerLitre = (litres, km) => km / litres;
console.log(litresPer100(60, 500)); // 12
console.log(kmPerLitre(60, 500));   // 8.333333333333334

It scores higher on liters per 100 km (12 against 9) and lower on kilometers per liter (8.33 against 11.11). Same car, same trip, opposite directions.

Step 4: the cost of the trip

With fuel at ₦900 a liter, the cost is liters times price. Keep money in kobo (100 kobo make one naira) so it stays a whole number:

Step 4

const trip = { km: 500, litres: 45 };
console.log(trip.litres * 900_00); // 4050000

4,050,000 kobo is ₦40,500. The underscore in 900_00 only makes it easier to read.

Step 5: when one side is zero

Real data has gaps. A trip logged before the car moved has a distance of 0. A failed fuel reading gives 0 liters:

JavaScript

const litresPer100 = (litres, km) => (litres / km) * 100;
const kmPerLitre = (litres, km) => km / litres;
console.log(litresPer100(45, 0)); // Infinity
console.log(kmPerLitre(0, 500));  // Infinity

Dividing by zero in JavaScript doesn't throw an error. It gives Infinity, which then spreads quietly into averages and charts. Check for a zero before you divide, wherever the numbers come from outside your code.

The full solution

The answer panel has both functions, prints both measures and the trip cost, and shows the zero-distance result. Its checks cover the reciprocal relationship, the thirstier car on both measures, and both Infinity cases. Press Run to see the output.

The answer

const litresPer100 = (litres, km) => (litres / km) * 100;
const kmPerLitre = (litres, km) => km / litres;

const trip = { km: 500, litres: 45 };
const round2 = (n) => Number(n.toFixed(2));

console.log('litres/100km ->', litresPer100(trip.litres, trip.km));
console.log('km/litre     ->', round2(kmPerLitre(trip.litres, trip.km)));
console.log('cost at 900 naira a litre ->', trip.litres * 900_00, 'kobo');
console.log('zero distance ->', litresPer100(45, 0));

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('nine litres per hundred kilometres', litresPer100(trip.litres, trip.km), 9);
test('eleven and a bit kilometres per litre', round2(kmPerLitre(trip.litres, trip.km)), 11.11);
test('the two are reciprocals, scaled by 100', round2(100 / litresPer100(45, 500)), round2(kmPerLitre(45, 500)));
test('a thirstier car scores higher on one measure', litresPer100(60, 500) > litresPer100(45, 500), true);
test('and lower on the other', kmPerLitre(60, 500) < kmPerLitre(45, 500), true);
test('fuel for the trip, in kobo', trip.litres * 90000, 4050000);
test('no distance travelled gives Infinity, not an error', litresPer100(45, 0) === Infinity, true);
test('no fuel used gives Infinity the other way', kmPerLitre(0, 500) === Infinity, true);

The same trip, two numbers, opposite directions of "better". Mixing them up in a comparison table is a classic reporting bug: sort by the wrong column and the most economical car appears worst. Name the variable after its unit — litresPer100, not consumption — and the mistake becomes hard to make.

Both divisions can take a zero denominator from real data, a trip logged before it started or a tank reading that failed. Infinity propagates quietly, so guard where you divide, as exercise 56 did.

Common mistakes

  • Forgetting the times 100. litres / km alone is liters per kilometer, a hundred times smaller than the usual figure.
  • Sorting by the wrong measure. Lower is better for L/100km, higher is better for km/L.
  • Not guarding against zero. A zero distance gives Infinity without any error.

Try it yourself

  1. Work out L/100km for a 320 km trip that used 24 liters.
  2. Write safeLitresPer100(litres, km) that returns null when km is 0.
  3. Given an array of trips, find the one with the lowest L/100km using reduce.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.