我的程序效率不高.有没有更短的方法来解决它?



由于我刚刚开始Javascript,我正在尝试解决一些问题并改善自己。我遇到了这样的问题。我试图从问题中逐步完成所有操作,但我认为我的代码效率不高。有人可以帮我开发它吗?

问题:

请为一个JavaScript函数编写代码,该函数接受两个参数:停放"日期和时间"(时间戳)并返回"日期和时间"(时间戳)。 在机场停车,适用以下规则。

停车费;

  • 前20分钟2欧元,

  • 升至 4 欧元,最长 40 分钟,

  • 最高一小时上涨至 6 欧元,

  • 上涨至7欧元,最多两小时,

  • 最多三个小时涨到9欧元,

  • 上涨至11欧元,最多四个小时,

  • 4-8小时上涨至13欧元,以及

  • 8-24小时上涨至15欧元。

  • 前 24 小时 16 欧元,之后每增加一天 收费 9 欧元。

这是我的代码:

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"))

您可以重构它以使用数组。然后,如果费用发生变化,您无需在代码中摆弄,只需更改该数组中的价格即可:

const steps = [
{ limit:       20, fee:  2 }, // The limits are in minutes
{ limit:       40, fee:  4 },
{ limit:       60, fee:  6 },
{ limit:   2 * 60, fee:  7 },
{ limit:   3 * 60, fee:  9 },
{ limit:   4 * 60, fee: 11 },
{ limit:   8 * 60, fee: 13 },
{ limit:  24 * 60, fee: 15 }, // For complex rules, use a function:
{ limit: Infinity, fee: minutes => 9 * Math.ceil(minutes / 24 / 60) + 7 }
];
// Converts a date string to a number of minutes since 1970-01-01
const dateToMinutes = str => Math.floor(new Date(str).getTime() / 60000);
const calcFee = (parkingDate, returnDate) => {
const minutesParked = dateToMinutes(returnDate) - dateToMinutes(parkingDate);
for (let step of steps) {
if (minutesParked <= step.limit) {
return isNaN(step.fee) ? step.fee(minutesParked) : step.fee;
}
}
};
// Just for testing
const test = (x, y) => (document.body.innerHTML += `<p>${x} - ${y}<br><b>${formatDuration(x, y)}: €${calcFee(x, y)}</b></p>`); const formatDuration = (x, y) => { const d = dateToMinutes(y) - dateToMinutes(x); const res = {d: Math.floor(d / 24 / 60), h: Math.floor((d % (24 * 60)) / 60), m: d % 60}; return `${res.d} days ${res.h} hours ${res.m} min`; };
test("2020-05-12 18:30:00", "2020-05-12 18:40:00");
test("2020-05-12 18:30:00", "2020-05-12 19:00:00");
test("2020-05-12 18:30:00", "2020-05-12 19:30:00");
test("2020-05-12 18:30:00", "2020-05-13 18:29:00");
test("2020-05-12 18:30:00", "2020-05-13 18:31:00");
test("2020-05-12 18:30:00", "2020-05-18 18:30:00");

最新更新