Back to Freecodecamp

Challenge 39: Fill The Tank

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68b06e589bf2273243814777.md

latest1.3 KB
Original Source

--description--

Given the size of a fuel tank, the current fuel level, and the price per gallon, return the cost to fill the tank all the way.

  • tankSize is the total capacity of the tank in gallons.
  • fuelLevel is the current amount of fuel in the tank in gallons.
  • pricePerGallon is the cost of one gallon of fuel.
  • The returned value should be rounded to two decimal places in the format: "$d.dd".

--hints--

costToFill(20, 0, 4.00) should return "$80.00".

js
assert.equal(costToFill(20, 0, 4.00), "$80.00");

costToFill(15, 10, 3.50) should return "$17.50".

js
assert.equal(costToFill(15, 10, 3.50), "$17.50");

costToFill(18, 9, 3.25) should return "$29.25".

js
assert.equal(costToFill(18, 9, 3.25), "$29.25");

costToFill(12, 12, 4.99) should return "$0.00".

js
assert.equal(costToFill(12, 12, 4.99), "$0.00");

costToFill(15, 9.5, 3.98) should return "$21.89".

js
assert.equal(costToFill(15, 9.5, 3.98), "$21.89");

--seed--

--seed-contents--

js
function costToFill(tankSize, fuelLevel, pricePerGallon) {

  return tankSize;
}

--solutions--

js
function costToFill(tankSize, fuelLevel, pricePerGallon) {
  let gallonsNeeded = tankSize - fuelLevel;
  let cost = gallonsNeeded * pricePerGallon;
  return `$${cost.toFixed(2)}`;
}