re: Daily Challenge #106 - Average Fuel Consumption VIEW POST

FULL DISCUSSION
 

typescript

interface Reading {
  centilitersPerKilometer: number;
  km: number;
}

const roundToPlaces = (places: number, value: number) =>
  +(Math.round(((value + `e+${places}`) as unknown) as number) + `e-${places}`);

const avgFuelConsumption = (start: Reading, end: Reading) => {
  const beforeConsumptionInCentiliters =
    start.km * start.centilitersPerKilometer;

  const afterConsumptionInCentiliters = end.km * end.centilitersPerKilometer;

  const consumptionInCentiliters =
    afterConsumptionInCentiliters - beforeConsumptionInCentiliters;

  const distanceInKilometers = end.km - start.km;

  const centilitersPerKilometer =
    consumptionInCentiliters / distanceInKilometers;

  return roundToPlaces(1, centilitersPerKilometer);
};
code of conduct - report abuse