SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no 'nested if then' statement in javascript.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mrhoo View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whteout View Post
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whteout View Post
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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;
        };
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whteout View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •