What I envisage is to use a config array, so that you can easily control how the prices are charged:
{
zoneCharge: [0, 5, 10, 15],
timeGroups: [4, 10, 20, 999],
timeCharge: [2, 3, 5, 8]
}
With zoneCharge, zone 1 is $5, zone 2 is $10, and so on.
With timeGroups, that’s the number of minutes before which the appropriate charge is added
And with timeCharge, that’s the cost at the appropriate number of minutes.
So with the 14 minutes being less than 20, a charge of $5 applies for that.
I already have a working sample of the form. The purpose of these questions is to narrow down on how you intend for it to be used.
<html>
<head>
</head>
<body>
<form id="taxiFare">
<p><label>Start: <input name="start"></label></p>
<p><label>End: <input name="end"></label></p>
<p><label>Minutes: <input name="minutes"></label></p>
<p><input type="submit"></p>
</form>
<div id="fareprice"></div>
<script src="calculateFare.js"></script>
<script>
var form = document.getElementById('taxiFare'),
result = document.getElementById('fareprice');
form.onsubmit = calculateFare(result, {
zoneCharge: [0, 5, 10, 15],
timeGroups: [4, 10, 20, 999],
timeCharge: [2, 3, 5, 8]
});
</script>
</body>
</html>
calculateFare.js
function calculateFare(fare, config) {
function zoneCharge(start, end) {
var min = Math.min(start, end),
max = Math.max(start, end),
i,
cost = 0;
for (i = min; i <= max; i += 1) {
cost += config.zoneCharge[i];
}
return cost;
}
function minutesCharge(minutes) {
var i,
cost;
for (i = 0; i < config.timeGroups.length; i += 1) {
if (minutes < config.timeGroups[i]) {
cost = config.timeCharge[i];
break;
}
}
return cost;
}
function update(fare, cost) {
while (fare.hasChildNodes()) {
fare.removeChild(fare.firstChild);
}
fare.appendChild(document.createTextNode('Fare: $' + cost.toFixed(2)));
}
return function () {
var form = this,
start = Number(form.elements.start.value) || 0,
end = Number(form.elements.end.value) || 0,
minutes = Number(form.elements.minutes.value) || 0,
total = 0;
total += zoneCharge(start, end);
total += minutesCharge(minutes);
update(fare, total);
return false;
};
}