Fill The Tank - FreeCodeCamp #39 Daily Challenge
1 min
Fill The Tank - Analysis & Explanation 🚗⛽
Problem Statement
Given a fuel tank size, the current fuel level, and the price per gallon, return the cost to fill the tank.
tankSize: Total tank capacity (gallons)fuelLevel: Current fuel in the tank (gallons)pricePerGallon: Price per gallon- The result must be a string in monetary format, with two decimals: “$d.dd”
Initial Analysis
The challenge is to calculate how many gallons are needed to fill the tank and multiply that value by the price per gallon. The result should be formatted as a dollar amount with two decimals.
Test Cases
| tankSize | fuelLevel | pricePerGallon | Expected Output |
|---|---|---|---|
| 15 | 5 | 3.50 | ”$35.00” |
| 10 | 10 | 4.00 | ”$0.00” |
| 20 | 0 | 2.75 | ”$55.00” |
| 15 | 5 | 0.00 | ”$0.00” |
| 10 | 12 | 3.00 | ”$0.00” |
| 12.5 | 7.3 | 4.20 | ”$21.84” |
| 15.75 | 10.5 | 3.80 | ”$19.95” |
| 20 | 5 | 2.95 | ”$44.25” |
| 18.5 | 9.75 | 3.65 | ”$31.19” |
Solution Development
Strategy
- Calculate missing gallons:
- If or , return “$0.00”
- Calculate
- Format the result to two decimals with a dollar sign
JavaScript Implementation
function fillTheTank(tankSize, fuelLevel, pricePerGallon) {
// Calculate missing gallons
const missing = tankSize - fuelLevel
// If no fuel needed or price is zero
if (missing <= 0 || pricePerGallon === 0)
return '$0.00'
// Calculate total cost
const cost = missing * pricePerGallon
// Format to two decimals
return `$${cost.toFixed(2)}`
}Usage Example
fillTheTank(15, 5, 3.50) // "$35.00"
fillTheTank(10, 10, 4.00) // "$0.00"
fillTheTank(12.5, 7.3, 4.20) // "$21.84"Complexity Analysis
- Time: (simple arithmetic operations)
- Space: (only scalar variables)
Edge Cases & Considerations
- If
fuelLevel >= tankSize→ “$0.00” - If
pricePerGallon === 0→ “$0.00” - Supports decimal values
- Assumes valid, non-negative inputs
Reflections & Learnings
- Practice with basic arithmetic and string formatting
- Robustness through edge case validation
- The problem is straightforward and needs no optimizations