-1

Here is the javascript code: There is an error in code where nightSurcharges is added to total cost even if pickUptime is less than 20.

function TaxiFare() {

    var baseFare = 2;
    var costPerMile = 0.50;
    var nightSurcharge = 0.50; // 8pm to 6am, every night //its flat 0.50 and not per mile
    var milesTravelled = Number(document.getElementById("miles").value) || 0;
    if ((milesTravelled < 1) || (milesTravelled > 200)) {
        alert("You must enter 1 - 200 miles");
        document.getElementById("miles").focus();
        return false;
    }

    var pickupTime = Number(document.getElementById("putime").value) || 0;
    if ((pickupTime == "") || (pickupTime < 0) || (pickupTime > 23)) {
        alert("The time must be 0-23 hours");
        document.getElementById("putime").focus();
        return false;
    }

    var cost = baseFare + (costPerMile * milesTravelled);

    // add the nightSurcharge to the cost if it is after
    // 8pm or before 6am
    if (pickupTime >= 20 || pickupTime < 6) {
        cost += nightSurcharge;
    }
    alert("Your taxi fare is $" + cost.toFixed(2));
}

I want nightSurcharge to be added only when pickupTime is >=20, but that's not working right now. Any help is appreciated. Thanks

4
  • 4
    What have you tried to debug this problem? It sounds like you're just posting the problem and expecting someone else to fix it. Commented Apr 17, 2012 at 15:17
  • 1
    if (pickupTime >= 20 || pickupTime < 6) { cost += nightSurcharge; } "I want nightSurcharge to be added only when pickupTime is >=20" wait a sec r u trying to homework me Commented Apr 17, 2012 at 15:24
  • what is the exact error, and wich numbers did you use to produce this error? Commented Apr 17, 2012 at 15:26
  • 1
    The comment explains the code: add the night surcharge if it's after 2000 or before 0600. That seems reasonable. Are you saying that the surcharge should not apply after midnight? Commented Apr 17, 2012 at 15:29

2 Answers 2

1

This seems obvious to me.

if (pickupTime >= 20 || pickupTime < 6) {
    cost += nightSurcharge;
}

This code right here adds nightSurcharge to the cost if pickupTime is greater than or equal to 20, OR less than 6. So of course it's added if it's less than 6.

if (pickupTime >= 20) {
    cost += nightSurcharge;
}

Now it will only add to it if it's greater or equal to 20.

Sign up to request clarification or add additional context in comments.

Comments

0

your code is:

if (pickupTime >= 20 || pickupTime < 6)

so if pickupTime is less then 6 it'll enter the if as well

http://jsfiddle.net/7rdzC/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.