错误消息而不是NaN输出和负“净付款”输出

时间:2017-05-01 09:07:27

标签: javascript

var vDiscountPercent;
var vGrossPayment;
var vTicketType;
vTicketType = prompt(" What type of Tickets is required?");
document.write("The Ticket Type is: " + vTicketType);

var vTicketQty;
vTicketQty = prompt("How many Tickets are required?");
document.write("<br/>");
document.write("The Ticket Qty is: " + vTicketQty + "<br/>");
var vTicketQty = parseInt(vTicketQty);

if (isNaN(parseInt(vTicketQty))) {
    vTicketQty = 0;
}

if (vTicketQty <= 0) {
    document.write("Invalid Qty" + "<br/>");
} else {
    vTicketPrice = calcPrice(vTicketType);
    if (vTicketPrice == -1) {
        document.write("Invalid Ticket Type" + "<br/>");
    } else {
        document.write("Ticket Price is: " + vTicketPrice + "<br/>");
    }
    var vTotalPayment = calcTotal(vTicketPrice, vTicketQty);
    if (vTotalPayment > 0) {
        document.write("Total payment required is: $" + vTotalPayment + "<br/>");
    } else {
        document.write("Invalid data supplied" + "<br/>");
    }
}

function calcTotal(vTicketPrice, vTicketQty) {
    return vTicketPrice * vTicketQty;
}

function calcPrice(vTicketType) {
    var vTicketPrice;
    if (vTicketType.toLowerCase() == "a") {
        vTicketPrice = 100;
    } else if (vTicketType.toLowerCase() == "b") {
        vTicketPrice = 75;
    } else if (vTicketType.toLowerCase() == "c") {
        vTicketPrice = 50;
    } else {
        vTicketPrice = -1;
    }
    return vTicketPrice;
}

vGrossPayment = vTotalPayment;
var vGrossPayment = calcDiscountPercent(vGrossPayment);
function calcDiscountPercent(vGrossPayment) {
if (vGrossPayment < 200) {
    document.write("Discount Percent: 0%");
    vDiscountPercent = 0;
}   else if (vGrossPayment < 400) {
    document.write("Discount Percent: 5%");
    vDiscountPercent = 0.05;
}   else if (vGrossPayment < 600) {
    document.write("Discount Percent: 7.5%");
    vDiscountPercent = 0.075;
}   else if (vGrossPayment > 600) {
    document.write("Discount Percent: 10%");
    vDiscountPercent = 0.10;
}   else {
    document.write("No discount");
} 
    return vDiscountPercent;
}

var applyDiscount = applyDiscount(vTotalPayment, vDiscountPercent);
function applyDiscount(vTotalPayment, vDiscountPercent) {
    return vTotalPayment * (vDiscountPercent * 100) / 100;
}
document.write("<br/>" + "Discount Amount: $" + applyDiscount);

var vNettPayment = calcDiscountAmount(vTotalPayment, applyDiscount);
function calcDiscountAmount(vGrossPayment, vDiscountPercent) {
    return vTotalPayment - applyDiscount;
}
document.write("<br/>" + "Nett Payment: $" + vNettPayment);

当我在'vTicketQty'中输入1到100之间的值时,'Nett Payment'和'Discount Amount'的输出为'NaN',如何获得输出值以显示错误消息而不是NaN? 此外,当我没有为'vTicketType'输入'a','b'或'c'时,'Nett Payment'的输出为负,再次如何让输出显示错误信息呢?

1 个答案:

答案 0 :(得分:1)

像这样使用。

var applyDiscount = applyDiscount(vTotalPayment, vDiscountPercent)|0;
var vNettPayment = calcDiscountAmount(vTotalPayment, applyDiscount)|0;

变量无效,它返回'0'

var vDiscountPercent;
var vGrossPayment;
var vTicketType;
vTicketType = prompt(" What type of Tickets is required?");
document.write("The Ticket Type is: " + vTicketType);

var vTicketQty;
vTicketQty = prompt("How many Tickets are required?");
document.write("<br/>");
document.write("The Ticket Qty is: " + vTicketQty + "<br/>");
var vTicketQty = parseInt(vTicketQty);

if (isNaN(parseInt(vTicketQty))) {
    vTicketQty = 0;
}

if (vTicketQty <= 0) {
    document.write("Invalid Qty" + "<br/>");
} else {
    vTicketPrice = calcPrice(vTicketType);
    if (vTicketPrice == -1) {
        document.write("Invalid Ticket Type" + "<br/>");
    } else {
        document.write("Ticket Price is: " + vTicketPrice + "<br/>");
    }
    var vTotalPayment = calcTotal(vTicketPrice, vTicketQty);
    if (vTotalPayment > 0) {
        document.write("Total payment required is: $" + vTotalPayment + "<br/>");
    } else {
        document.write("Invalid data supplied" + "<br/>");
    }
}

function calcTotal(vTicketPrice, vTicketQty) {
    return vTicketPrice * vTicketQty;
}

function calcPrice(vTicketType) {
    var vTicketPrice;
    if (vTicketType.toLowerCase() == "a") {
        vTicketPrice = 100;
    } else if (vTicketType.toLowerCase() == "b") {
        vTicketPrice = 75;
    } else if (vTicketType.toLowerCase() == "c") {
        vTicketPrice = 50;
    } else {
        vTicketPrice = -1;
    }
    return vTicketPrice;
}

vGrossPayment = vTotalPayment;
var vGrossPayment = calcDiscountPercent(vGrossPayment);
function calcDiscountPercent(vGrossPayment) {
if (vGrossPayment < 200) {
    document.write("Discount Percent: 0%");
    vDiscountPercent = 0;
}   else if (vGrossPayment < 400) {
    document.write("Discount Percent: 5%");
    vDiscountPercent = 0.05;
}   else if (vGrossPayment < 600) {
    document.write("Discount Percent: 7.5%");
    vDiscountPercent = 0.075;
}   else if (vGrossPayment > 600) {
    document.write("Discount Percent: 10%");
    vDiscountPercent = 0.10;
}   else {
    document.write("No discount");
} 
    return vDiscountPercent;
}

var applyDiscount = applyDiscount(vTotalPayment, vDiscountPercent)|0;
function applyDiscount(vTotalPayment, vDiscountPercent) {
    return vTotalPayment * (vDiscountPercent * 100) / 100;
}
if(applyDiscount){
document.write("<br/>" + "Discount Amount: $" + applyDiscount)
}
else{
document.write("<br/>Error-something not right in Discount Amount")
}

var vNettPayment = calcDiscountAmount(vTotalPayment, applyDiscount)|0;
function calcDiscountAmount(vGrossPayment, vDiscountPercent) {
    return vTotalPayment - applyDiscount;
}
if(vNettPayment){
document.write("<br/>" + "Nett Payment: $" + vNettPayment);
}
else{
document.write("<br/>Error-something not right in Nett Payment ")
}