首页 > 解决方案 > 我的程序效率不高。有没有更短的方法来解决它?

问题描述

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

问题:

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

停车费;

这是我的代码:

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

标签: javascript

解决方案


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

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


推荐阅读