Since I have just started Javascript, I am trying to solve a few problems and improve myself. I encountered such a question. I tried to do everything step by step from the question, but I do not think that my code is efficient. Can someone help me develop it?
Please code for a JavaScript function that accepts two arguments that are; parking "date and time"(timestamp) and return "date and time"(timestamp). At an airport for car parking, the following rules are applied.
Parking costs;
€2 for the first 20 minutes,
rising to €4 for up to 40 minutes,
rising to €6 for up to one hour,
rising to €7 for up to two hours,
rising to €9 for up to three hours,
rising to €11 for up to four hours,
rising to €13 for 4-8 hours and
rising to €15 for 8-24 hours.€16 for the first 24 hours and after that each additional day is charged at €9.
Here is my code:
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"))