Starting off with my journey in Javascript, I am eager to tackle problems and enhance my skills. One challenge that came my way is the following question. Despite my efforts to solve it step by step, I feel like there might be room for improvement in my code. Is there anyone out there who can assist me in refining it?
QUERY:
I am seeking a JavaScript function that takes two arguments - the date and time of parking (timestamp) and returns the date and time of returning (timestamp). The scenario involves car parking at an airport, which follows certain pricing rules as outlined below.
Parking charges;
€2 for the first 20 minutes,
increasing to €4 for up to 40 minutes,
increasing further to €6 for one hour,
updating to €7 for two hours,
and then escalating to €9 for three hours,
which goes up to €11 for four hours,
further rises to €13 for 4-8 hours,
and finally hits €15 for 8-24 hours.
A flat rate of €16 applies for the initial 24 hours, with each additional day incurring a charge of €9.
This is the current code I have attempted:
function msToHours(milisecond) {
let time = milisecond;
let hour = (time / 60000) / 60;
return hour;
}
//Mins to Hours Function
function minToHours(miniute){
let time = miniute;
let hr = (miniute /60);
return hr;
}
//Finding the nth day Function
function add24HR(hour, cb) {
let arr = new Array();
for (let i = 0; i < hour; i++) {
if (i % 24 == 0) {
arr.push(i)
}
}
return `Your Parking Fee is £${(arr.length*cb-cb) + 16}.(${arr.length} days)`
}
//Main Function
const parkingFees = (parkingDate, returnDate) => {
//Defining dates
var park = new Date(parkingDate)
var returned = new Date(returnDate);
//Variables
var penaltyFee = 9;
let totalPrice;
//Time between park and return (miliseconds)
let totalTime = returned - park
//MiliSeconds to Hour
let totalPark = msToHours(totalTime);
//Mins to Hours
if (totalPark <= minToHours(20)) {
return `Your parking fee is only £${2}.`
}
else if(totalPark > minToHours(20) && totalPark <= minToHours(40)){
return `Your parking fee is only £${4}.`
}
else if(totalPark > minToHours(40) && totalPark <= minToHours(60)){
return `Your parking fee is only £${6}.`
}
else if(totalPark > minToHours(60) && totalPark <= minToHours(120)){
return `Your parking fee is only £${7}.`
}
else if(totalPark > minToHours(120) && totalPark <= minToHours(180)){
return `Your parking fee is only £${9}.`
}
else if(totalPark > minToHours(180) && totalPark <= minToHours(240)){
return `Your parking fee is only £${11}.`
}
else if(totalPark > minToHours(240) && totalPark <= minToHours(480)){
return `Your fparking fee is only £${13}.`
}
else if(totalPark > minToHours(480) && totalPark < minToHours(1440)){
return `Your parking fee is only £${15}.`
}
else if(totalPark > minToHours(1440) && totalPark < minToHours(2880)){
return `Your parking fee is only £${16}.`
}
//if totalPark > 24 HRS
else {
totalPrice = add24HR(totalPark, penaltyFee)
}
return totalPrice;
}
document.querySelector("body").innerHTML = (parkingFees("5/12/2020 18:30", "5/18/2020 18:30"))