Back to Freecodecamp

Challenge 141: Takeoff Fuel

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69272dcf1c24b44fd79137c4.md

latest1.3 KB
Original Source

--description--

Given the numbers of gallons of fuel currently in your airplane, and the required number of liters of fuel to reach your destination, determine how many additional gallons of fuel you should add.

  • 1 gallon equals 3.78541 liters.
  • If the airplane already has enough fuel, return 0.
  • You can only add whole gallons.
  • Do not include decimals in the return number.

--hints--

fuelToAdd(0, 1) should return 1.

js
assert.equal(fuelToAdd(0, 1), 1);

fuelToAdd(5, 40) should return 6.

js
assert.equal(fuelToAdd(5, 40), 6);

fuelToAdd(10, 30) should return 0.

js
assert.equal(fuelToAdd(10, 30), 0);

fuelToAdd(896, 20500) should return 4520.

js
assert.equal(fuelToAdd(896, 20500), 4520);

fuelToAdd(1000, 50000) should return 12209.

js
assert.equal(fuelToAdd(1000, 50000), 12209);

--seed--

--seed-contents--

js
function fuelToAdd(currentGallons, requiredLiters) {

  return currentGallons;
}

--solutions--

js
function fuelToAdd(currentGallons, requiredLiters) {
  const litersPerGallon = 3.78541;
  const currentLiters = currentGallons * litersPerGallon;
  if (currentLiters >= requiredLiters) return 0;
  const litersNeeded = requiredLiters - currentLiters;
  return Math.ceil(litersNeeded / litersPerGallon);
}