javascript - 我的程序效率不高。有没有更短的方法来解决它?
问题描述
由于我刚刚开始使用 Javascript,因此我正在尝试解决一些问题并提高自己。我遇到了这样一个问题。我试图从问题一步一步做所有事情,但我认为我的代码效率不高。有人可以帮我开发吗?
问题:
请编写一个接受两个参数的 JavaScript 函数:停车“日期和时间”(时间戳)并返回“日期和时间”(时间戳)。在机场停车场,适用以下规则。
停车费;
前 20 分钟 2 欧元,
最多 40 分钟升至 4 欧元,
长达一小时升至 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");
推荐阅读
- typescript - Action-Reducer 映射具有多种操作类型的类型错误
- jboss-eap-6 - 引起:java.lang.IllegalStateException:前缀ns3未绑定
- angular - 使用Angular 8的两行标题的Mat-table
- opengl - 获取每个原语中的片段数
- vim - 如何删除数字后的字符直到结尾
- python - 如何根据单个用户输入构建二叉树(广度优先或级别顺序)?
- python - python:在数据框列表中按名称对列进行子集和重命名
- python - 在循环运行后使用字典的值更新多个键并更新用于更新字典的值
- r - 如何告诉 R Markdown 当前块返回 Latex 代码
- java - 如何将经典的 cronjobs 转换为quartz-scheduler 的 cron 表达式