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

由于我刚刚开始使用Javascript,我正在尝试解决一些问题并提高自己。我就遇到过这样的问题。我试图从问题中一步一步地做所有事情,但我不认为我的代码是有效的。有人可以帮我开发它吗?

问题:

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

停车费;

  • 前 20 分钟 2 欧元,

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

  • 最多一小时升至 6 欧元,

  • 最多两小时内升至 7 欧元,

  • 涨至 9 欧元,持续长达三个小时,

  • 涨至 11 欧元,持续时间长达 4 小时,

  • 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}.`

        }


素胚勾勒不出你
浏览 117回答 1
1回答

SMILET

您可以重构它以使用数组。然后,如果费用发生变化,您无需修改代码,只需更改该数组中的价格即可:const steps = [&nbsp; { limit:&nbsp; &nbsp; &nbsp; &nbsp;20, fee:&nbsp; 2 }, // The limits are in minutes&nbsp; { limit:&nbsp; &nbsp; &nbsp; &nbsp;40, fee:&nbsp; 4 },&nbsp; { limit:&nbsp; &nbsp; &nbsp; &nbsp;60, fee:&nbsp; 6 },&nbsp; { limit:&nbsp; &nbsp;2 * 60, fee:&nbsp; 7 },&nbsp; { limit:&nbsp; &nbsp;3 * 60, fee:&nbsp; 9 },&nbsp; { limit:&nbsp; &nbsp;4 * 60, fee: 11 },&nbsp; { limit:&nbsp; &nbsp;8 * 60, fee: 13 },&nbsp; { limit:&nbsp; 24 * 60, fee: 15 }, // For complex rules, use a function:&nbsp; { limit: Infinity, fee: minutes => 9 * Math.ceil(minutes / 24 / 60) + 7 }];// Converts a date string to a number of minutes since 1970-01-01const dateToMinutes = str => Math.floor(new Date(str).getTime() / 60000);const calcFee = (parkingDate, returnDate) => {&nbsp; const minutesParked = dateToMinutes(returnDate) - dateToMinutes(parkingDate);&nbsp; for (let step of steps) {&nbsp; &nbsp; if (minutesParked <= step.limit) {&nbsp; &nbsp; &nbsp; return isNaN(step.fee) ? step.fee(minutesParked) : step.fee;&nbsp; &nbsp; }&nbsp; }};// Just for testingconst 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");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript