这个加时赛总计没有正确计算的原因是什么?

时间:2015-06-10 04:08:31

标签: javascript php html css

这里有一点解释。一个停车库收费5.00美元停车长达三个小时。车库每小时额外收费1.50美元,或每小时超过3小时。任何给定24小时的最高费用为18.00美元。我们假设一次停车的时间不超过24小时。

3小时以上总数不计算是否有任何理由?我觉得它与sum变量有关,但我不能把手指放在它上面。               

<script type="text/javascript">

function multiply() {

var hoursParked = document.getElementById('hP').value;
var price = 5.00;
var totalCost = document.getElementById('total');
var totalPayment = (hoursParked * price);
    if (hours < 3) {
    sum = price * hours;
    return sum;   
}
else {
    sum = ((hours - 3) * 0.5 * price) + (price * hours);
    return sum;
} 
totalCost.value= sum; }

</script>
</head>


<body><div align="center">
<form id="1">
<input type="text" id="customerName" name="customerName" placeholder="Please enter your name here." size="27px"><br><br>
<input type="number" id="hP" name="hoursParked" placeholder="Hours parked?" min="1" max="24"><br><br>
<input type="button" id="calculateP" name="calculatePayment" value="Calculate" onclick="multiply()"/>
<input type="reset" id="resetBtn" value="Clear"><br><br>
<input type="number" id="total" name="totalCost" placeholder="Your Total Payment" readonly/><hr>
</form>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

function multiply() {

var hoursParked = document.getElementById('hP').value;
var price = 5.00;
var totalCost = document.getElementById('total');
var totalPayment = (hoursParked * price);
    if (hoursParked <= 3) {
    sum = price * hoursParked;
    return sum;   
}
else {
    sum = ((hoursParked - 3) * (price + 1.50)) + (price * 3);
    return sum;
} 
totalCost.value= sum; }

此功能将解决您的问题。试试这个

答案 1 :(得分:0)

请试试这个。

function multiply() {

                    var hoursParked = document.getElementById('hP').value;
                    var price = 5.00;
                    var totalCost = document.getElementById('total');
                    var totalPayment = (hoursParked * price);
                    var sum="";
                    if (hoursParked <= 3) {
                    sum = price * hoursParked;

                    }
                    else {
                     sum = ((hoursParked - 3) * (price + 1.50)) + (price * 3);

                    } 
                    $('#total').val(sum) ;
                }

答案 2 :(得分:0)

假设您在前3个小时内总计$ 5.00(而不是每小时$ 5.00) 假设你的意思是小时后每小时1.50美元(而不是每小时6.50美元)

function multiply() {

    var hoursParked = document.getElementById('hP').value;
    var totalCost = document.getElementById('total');
    totalCost.value = getTotalPayment(hoursParked);
}

function getTotalPayment(hoursParked) {
   var basePrice = 5.00;
   var baseHours = 3;
   var maxPayment = 18.00;
   var hourlyRate = 1.50;
   return Math.min(
       basePrice +
       (Math.max(Math.ceil(hoursParked - baseHours), 0) * hourlyRate), 
       maxPayment);
}

我已经考虑了getTotalPayment()函数,因此您可以使用一些alert()语句快速尝试。以下是一些测试用例:

//should all display 5.00
alert(getTotalPayment(0.00001));
alert(getTotalPayment(1));
alert(getTotalPayment(2));
alert(getTotalPayment(3));

//should display 6.50
alert(getTotalPayment(3.01));
alert(getTotalPayment(3.9));
alert(getTotalPayment(4));

//should display 8
alert(getTotalPayment(5));

//should display 9.50
alert(getTotalPayment(5.01));
alert(getTotalPayment(5.999));

//should display 17
alert(getTotalPayment(11));

//should display 18
alert(getTotalPayment(11.1));
alert(getTotalPayment(12));
alert(getTotalPayment(24));

编辑:阅读OP的澄清评论后

假设前3个小时每小时5.00美元 假设其后的时间为每小时6.50美元

function getTotalPayment(hoursParked) {
   var initialRate = 5.00;
   var augmentedRate = 6.50;
   var baseHours = 3;
   var maxPayment = 18.00;
   hoursParked = Math.ceil(hoursParked);  //round up any fractional hour
   return Math.min(
       //cost for any hours up to 3
       Math.min(3, hoursParked) * initialRate +
       //cost for any hours beyond 3
       Math.max(hoursParked - baseHours, 0) * augmentedRate, 
       maxPayment);
}

以及具有这些新假设的函数的一些测试用例:

//should return 5
alert(getTotalPayment(0.00001));
alert(getTotalPayment(1));

//should return 10
alert(getTotalPayment(1.5));
alert(getTotalPayment(2));

//should return 15
alert(getTotalPayment(3));

//should return 18
alert(getTotalPayment(3.01));
alert(getTotalPayment(4));
alert(getTotalPayment(11.1));
alert(getTotalPayment(24));