# help with problem

• Jun 1, 2011, 20:12
whteout
help with problem
OK so I have a simple fare I have to compute. There is a start zone and an end zone. There are 3 separate zones so 9 different charges. Plus there are charges for minutes too 0-4, 4-10, 10-20 and 20 to no limit. The user will input 3 parameters the start and end zone and the time. This would be a JS file. HELP

I am just trying to see how to start this and I know I need nested if then statements.
• Jun 1, 2011, 21:34
paul_wilkins
Will it always be only 3 inputs that are made?
Will a form that consists of only a start, end, and time fields (as well as possibly a result area) be enough?
Can you provide an example of how the start/end zone will be used to calculate the fare.
• Jun 1, 2011, 21:59
mrhoo
There is no 'nested if then' statement in javascript.
• Jun 1, 2011, 22:01
paul_wilkins
Quote:

Originally Posted by mrhoo
There is no 'nested if then' statement in javascript.

He may not know the correct words with which to impart that which he is meaning.
• Jun 2, 2011, 16:33
whteout
yes sorry i meant nested if else statements.

Example would be start 1 end 1 and 10 minutes or start 1 end 2 and 5 minutes would be your inputs.
• Jun 2, 2011, 16:36
paul_wilkins
Quote:

Originally Posted by whteout
Example would be start 1 end 1 and 10 minutes or start 1 end 2 and 5 minutes would be your inputs.

What effect do the zones have on the charges, and how do the minutes factor in to things?
• Jun 2, 2011, 16:53
whteout
each example of a start and end has a price. time is split into 15 minute intervals and increase in price. so 1 to 1 is \$5 and 14 minutes is \$5 so your fare is \$10
• Jun 2, 2011, 17:30
paul_wilkins
Quote:

Originally Posted by whteout
each example of a start and end has a price. time is split into 15 minute intervals and increase in price. so 1 to 1 is \$5 and 14 minutes is \$5 so your fare is \$10

So when going from zone 1 to zone 2 you add up both the charges for each zone plus the minutes?

What about when going from zone 1 to 3? Do you add all three zone charges together for that?
• Jun 2, 2011, 17:51
paul_wilkins
What I envisage is to use a config array, so that you can easily control how the prices are charged:

Code javascript:

```{ 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.

Code html4strict:

```<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
Code javascript:

```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; }; }```
• Jun 2, 2011, 17:59
whteout
sorry you have 3 zones. you ask the user for a start and an end zone. there are only 3 zones. so they start in 1 and end in 2 and that is a price. you start in 2 and end in 3 and that is a price. So there are 9 total choices. Then you have to ask the user for the amount of time it took and that is broken into 15 minute increments.
• Jun 2, 2011, 18:06
paul_wilkins
Quote:

Originally Posted by whteout
sorry you have 3 zones. you ask the user for a start and an end zone. there are only 3 zones. so they start in 1 and end in 2 and that is a price. you start in 2 and end in 3 and that is a price. So there are 9 total choices. Then you have to ask the user for the amount of time it took and that is broken into 15 minute increments.

That seems to differ from what was initially stated.
0-4, 4-10, 10-20 and 20 to no limit

When you have the specs nailed down, we can then advise you on the most appropriate techniques to use.