2017-05-01 12 views
2
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」の出力は「NaN」になります。と 'Discount Amount'では、出力値にNaNの代わりにエラーメッセージが表示されるようにするにはどうすればよいですか? また、 'vTicketType'に 'a'、 'b'または 'c'を入力しなかった場合、 'Nett Payment'の出力は負になります。代わりにエラーメッセージを表示するにはどうすればよいですか?NaN出力の代わりにエラーメッセージが表示され、「Nett Payment」の出力が負になる

答えて

1
このような

使用。

var applyDiscount = applyDiscount(vTotalPayment, vDiscountPercent)|0; 
var vNettPayment = calcDiscountAmount(vTotalPayment, applyDiscount)|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 ") 
 
}

+0

'0'

で返していますが、ありがとう、助け –

+0

あなたは 'vticketType'が 'a'、 'b'または 'c'でない場合、エラーメッセージを出すために 'Nett Payment'を得る方法や、0と表示される方法を知っていますか? –

+0

@エヴァン私の更新された答えを参照してください。あなたの願いとしてエラーmsgのテキストを変更することができます – prasanth

関連する問題