-1

This is the full question

Write a complete for charging the parking fee for each customer based on the number of hour parking. Ask user to enter the total hour using prompt()
Calculate the amount of charge that a customer has to pay using the function calcCharge().
This function receive the total hour or parking through its parameter. It then calculate and returns the amount of charges to the customer.
The formula to calculate the amount of charges to the customer : In the first hour, the charge is RM 1.00. In the next hour, the addition charge is RM 0.50 per hour. The maximum charge is RM 10.00. Calculate the total charge of all the customers using function calcTotal(). Display the amount of charges for a customer. Display total charge of all customer using alert()



and ive tried to do the code but fail.
i fail to display the total charge of a customer and total charge for all customers.
i hope somebody willing to help me, thanks!


<html>
<head><title>Calculate Parking Hours</title>

<script type="text/javascript">

function calcCharge()
{
 var hour=parseInt(document.fee.hour.value);
 var tot;
 if (hour==1)
  tot=1;
 else if (hour>1 && hour<=20)
  tot=1+0.5*(hour-1);
 else
  tot=10;
 
 
 document.write("The total charge is : RM "+tot);
  
}

function calcTotal()
{
 var totCha;
 totCha=parseInt(totCha);
 totCha+=document.tot.value;
 alert("Total charge all customers"+totCha);
}
</script>
</head>
<body onload="calcTotal()">
<form name="fee" method="post">
Hour : <input name="hour" type="text"><br>
<input type="submit" name="submit" value="Total Charge" onclick="calcCharge()">


</form>
</body>
</html>
Kyra Shin
  • 3
  • 3
  • Shouldn't your logic be `tot=1+0.5*(hour-1);` instead of `tot=1+0.5*hour;`..As for first hour, RM 1.00 is taken.. – Rakesh_Kumar Feb 27 '15 at 07:19
  • 1
    [parseDouble method not exist in JavaScript](http://stackoverflow.com/questions/21278234/does-parsedouble-exist-in-javascript), `var totCha; totCha=parseDouble(totCha);` - you try parse _undefined_ it any way **NaN** – Grundy Feb 27 '15 at 07:22
  • ive corrected the error, but how to calculate the total charge for all customers? – Kyra Shin Feb 27 '15 at 07:25
  • also see MDN:[Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open which will clear the document.](https://developer.mozilla.org/en-US/docs/Web/API/Document/write) – Grundy Feb 27 '15 at 07:26
  • @KyraShin Where are you taking input for number of customers? – Rakesh_Kumar Feb 27 '15 at 07:26
  • 1
    methinks you need move your question to [Code Review](http://codereview.stackexchange.com/) – Grundy Feb 27 '15 at 07:29
  • from the form field input – Kyra Shin Feb 27 '15 at 07:29
  • @KyraShin did you try with my code ? let me know if you have any difficulies – Arunprasanth K V Feb 27 '15 at 11:31
  • @ArunprasanthKV yup your code worked thank you !! – Kyra Shin Feb 28 '15 at 08:50

2 Answers2

1

Update the following line:

var hour = parseInt(document.getElementByName("hour").value);
Sarmin Akter
  • 120
  • 7
0

in your question you say calcCharge() function receive the total hour or parking through its parameter.The reality is you didn't pass any argument to your function. IF you want it in a right way then try the below code

html

<input type="button"onclick="calcCharge(document.getElementById('text1').value)"/>
<input type="text" id="text1"/>

js

function calcCharge(value)
{

var hour=parseInt(value);
 var tot=0;
if (hour==1)
         tot=1;
    else if (hour>1 && hour<=20)
{
        tot=1+(0.5*(hour-1));
}
    else
{
        tot=10;
}


    alert(tot);

}

DEMO

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71