我必须创建一个票价计算器,它还可以计算附加费率和金额。
我的票价开关有效,但在我在提示中输入大小写后,附加费率开关返回为 NaN。
我已将函数 surchargeRate(cardType) 返回的值转换为数字,但由于某种原因它仍然给我 NaN。
我一直试图找出问题所在,但无法弄清楚。
这是我的代码:
function surchargeRate(cardType) {
var surchargeRate;
switch (cardType) {
case "1":
surchargeRate = 0.02;
break;
case "2":
surchargeRate = 0.01;
break;
} // end switch
return surchargeRate;
}
// This function calculates the surcharge amount.
function surchargeAmount(ticketCost, surchargeRate) {
return ticketCost * surchargeRate;
}
function determineTicketPrice(seatingAreaCategory) {
var ticketPrice;
switch (seatingAreaCategory) {
case "S":
case "s":
ticketPrice = 180;
break;
case "C":
case "c":
ticketPrice = 150;
break;
case "B":
case "b":
ticketPrice = 100;
break;
case "R":
case "r":
ticketPrice = 70;
break;
default:
ticketPrice = 0;
break;
} // end switch
return ticketPrice;
}
function totalAmount(ticketPrice, ticketQty) {
return ticketPrice * ticketQty;
}
function validateQty(ticketQty) {
if (ticketQty >= 1 && ticketQty <= 20) {
return true;
} else {
alert("Ticket number must be between 1 and 20");
return false;
}
}
function init() {
surchargeRate = surchargeRate(cardType);
surchargeRate = Number(surchargeRate);
var surcharge;
var finalCost;
if (surchargeRate == -1) {
surchargeRate = new Error("Invalid input");
} else {
surcharge = surchargeAmount(ticketCost, surchargeRate);
}
白衣非少年
相关分类