-2

A little explanation here. A parking garage charges $5.00 to park for up to three hours. The garage charges an additional $ 1.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24 hour period is $ 18.00. We are assuming that no car parks for longer than 24 hours at a time.

Is there any reason why the total will not calculate when over 3 hours? I have a feeling it has something to do with the sum variable, but I can't put my finger on it.

<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>
lizardwizard
  • 189
  • 1
  • 2
  • 10
lizardwizard
  • 187
  • 1
  • 2
  • 10
  • 1
    Never use floating point to represent integers, specifically money (granted, everything is floating point in JS, but you get the point--no pun intended): http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Bailey Parker Jun 10 '15 at 04:10
  • Your `hours` variable is not initialised anywhere. You're using `hoursParked` –  Jun 10 '15 at 04:12
  • sum = ((hours - 3) * 1.5) + (price * 3); – samgak Jun 10 '15 at 04:12
  • Additionally, why are you returning `sum`? And what is the difference between `sum` and `totalPayment`? – Bailey Parker Jun 10 '15 at 04:13
  • "A parking garage charges $5.00 to park for up to three hours." Is that $5 per hour or $5 in total for the first 3 hours? – samgak Jun 10 '15 at 04:13
  • *side note:* multiple HTML errors spotted: 1. `id="1"` is invalid. It starts with alphabets; 2. `align="center"` is deprecated, use CSS styling instead. – Raptor Jun 10 '15 at 04:15
  • I guess I shouldn't be returning the sum, I want the sum to be calculated in totalCost – lizardwizard Jun 10 '15 at 04:15
  • 5 dollars per hour until the time parked exceeds 3 hours, then an additional 1.50 is added per hour. So 6.50 per hour. – lizardwizard Jun 10 '15 at 04:17
  • @jamcgraw Don't edit suggested changes into your question such that the question changes. It renders existing comments and answers confusing at best, useless at worst. If you need to make those changes, make it clear what you have changed, and where. I've rolled back your edits. –  Jun 10 '15 at 04:47
  • @jamcgraw, your clarification now has me scratching my head even more than your original wording.....so $5.00 per hour for the first 3 hours, and $6.50 per hour per hour thereafter?? So in hour 4, you will reach the $18 total daily limit?? – Richard II Jun 10 '15 at 05:43

3 Answers3

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; }

This function will be fix your problem. Try this one

Vignesh Bala
  • 889
  • 6
  • 25
0

Please try this one .

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) ;
                }
Chandan Sarma
  • 308
  • 2
  • 5
  • 19
0

Assuming you mean $5.00 total for the first 3 hours (and not $5.00 per hour)
Assuming you mean hours thereafter are $1.50 per hour (and not $6.50 per hour)

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

I've factored out the getTotalPayment() function so you can try it quickly with some alert() statements. Here are some test cases:

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

EDIT: after reading clarifying comments by OP

Assuming $5.00 per hour for the first 3 hours
Assuming $6.50 per hour for time thereafter

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

and some test cases for the function having these new assumptions:

//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));
Richard II
  • 853
  • 9
  • 31