curriculum/challenges/english/blocks/daily-coding-challenges-javascript/699c8e045ee7cb94ed2322d6.md
Given two strings representing the time you parked your car and the time you picked it up, calculate the parking fee.
"HH:MM" using a 24-hour clock. So "14:00" is 2pm for example.Fee rules:
Return the total cost in the format "$cost", "$5" for example.
calculateParkingFee("09:00", "11:00") should return "$6".
assert.equal(calculateParkingFee("09:00", "11:00"), "$6");
calculateParkingFee("10:00", "10:30") should return "$5".
assert.equal(calculateParkingFee("10:00", "10:30"), "$5");
calculateParkingFee("08:10", "10:45") should return "$9".
assert.equal(calculateParkingFee("08:10", "10:45"), "$9");
calculateParkingFee("14:40", "23:10") should return "$27".
assert.equal(calculateParkingFee("14:40", "23:10"), "$27");
calculateParkingFee("18:15", "01:30") should return "$34".
assert.equal(calculateParkingFee("18:15", "01:30"), "$34");
calculateParkingFee("11:11", "11:10") should return "$82".
assert.equal(calculateParkingFee("11:11", "11:10"), "$82");
function calculateParkingFee(parkTime, pickupTime) {
return parkTime;
}
function calculateParkingFee(parkTime, pickupTime) {
function toMinutes(time) {
const [hours, minutes] = time.split(":").map(Number);
return hours * 60 + minutes;
}
const entryMinutes = toMinutes(parkTime);
const exitMinutes = toMinutes(pickupTime);
let totalMinutes;
let overnight = false;
if (exitMinutes < entryMinutes) {
overnight = true;
totalMinutes = (24 * 60 - entryMinutes) + exitMinutes;
} else {
totalMinutes = exitMinutes - entryMinutes;
}
const hours = Math.ceil(totalMinutes / 60);
let cost = hours * 3;
if (overnight) {
cost += 10;
}
if (cost < 5) {
cost = 5;
}
return `$${cost}`;
}