Robin D.
Robin D.

Reputation: 97

Javascript Order Form - Calculating a Discount (getting error message)

I have to put together an order form in JS that calculates a 10-40% discount for every 10 "widgets" ordered. All the fields on my form validate (name, phone, amount ordered, etc) but when I click submit, I get the message "Please correct your input." Not sure what I'm missing! There must be some error in the function I created to calculate the discount. There's a LOT of code so I only included the last 2 functions in question. Any ideas would be awesome...thank you!

function calcDiscount (quantity) {

    if (quantity < 10)
        return 0;

    if (quantity < 20)
        return 0.1;

    if (quantity < 30)
        return 0.2;

    if (quantity < 40)
        return 0.3;

    if (quantity >= 40)
        return 0.4;

} // end function calcDiscount(quantity)

    function calcOrder() {
    if (validateFirstName() && validateLastName() && validatePhone() && validateQuantity() && calcDiscount(quantity)) {

        const TAXRATE = 0.085;
        var userName = document.getElementById("username").value;
        var userName2 = document.getElementById("username2").value;
        var quantity = document.getElementById("quantity").value;
        var cost = document.getElementById("cost").value;
        var extendedCost = quantity * cost;
        var taxAmount = extendedCost * TAXRATE;
        var discountAmount = calcDiscount(quantity) * extendedCost;
        var orderCost = extendedCost - discountAmount + taxAmount;
        document.getElementById("costExtended").value = "$" + orderCost.toFixed(2);

        document.getElementById("output").innerHTML += "<p>Hello " + userName + userName2 + " - Your order of " + quantity + " widgets, totals $" + orderCost.toFixed(2) + ", including tax" + "and a" + discountAmount + "% discount.</p>";
    } else {
        document.getElementById("costExtended").value = "";
        alert("Please correct your input");
    }
} // end function calcOrder

Upvotes: 1

Views: 75

Answers (1)

Action Dan
Action Dan

Reputation: 433

answering your question won't help anyone else because it is unique to you and the problem is just coding error and you not really knowing what you're doing.

It is obvious that to have the error pop up, then one of the following is returning false:

 validateFirstName() && validateLastName() && validatePhone() && validateQuantity() && calcDiscount(quantity)

However you've not included any of those functions so it's difficult to know what the cause of the problem is, but rather than you updating your question to include them I think you need to learn a bit more because the bug in one or all of those functions will be trivial and the answers you will need to solve that will already be here somewhere.

By the way, I suggest you refine your logic in your function that works out the discount because although your list of if's works, you can have just one line to work that out such as this:

 var discount = Math.floor(quantity/10)/10;

Upvotes: 1

Related Questions