1 回答
TA贡献1155条经验 获得超0个赞
默认值
var surchargeRate = 0;
或default: surchargeRate = 0;
和var ticketPrice = 0;
没有提升变量
var
- 函数windows.surchargeRate
被覆盖surchargeRate = surchargeRate(cardType);
Init 在没有任何输入的情况下进行大量计算 - 首先获取输入,然后使用它
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 = 0;
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() {
var beforeSurchargeOut = document.getElementById("beforeSurcharge");
var costOut = document.getElementById("amountDue");
var priceOut = document.getElementById("pricePerTicket");
var seatOut = document.getElementById("seatingArea");
var surchargeOut = document.getElementById("surcharge");
var validQtyOut = document.getElementById("ticketQty");
var surcharge;
var finalCost;
var seatingAreaCategory;
seatingAreaCategory = prompt("Please enter seating area category: ");
var ticketPrice = determineTicketPrice(seatingAreaCategory);
ticketPrice = Number(ticketPrice);
var ticketQty;
ticketQty = prompt("Please enter ticket quantity: ");
ticketQty = Number(ticketQty);
var ticketCost;
ticketCost = totalAmount(ticketPrice, ticketQty);
var cardType;
cardType = prompt("Enter your Card type. Enter 1 for American Express and 2 for Visa/Mastercard ");
var validQty = validateQty(ticketQty);
if (surcharge == -1) {
surcharge = new Error("Invalid input");
} else {
surcharge = surchargeAmount(ticketCost, surchargeRate(cardType));
}
finalCost = surcharge + ticketCost;
console.log(cardType);
console.log(surcharge);
beforeSurchargeOut.innerHTML = "Gross amount: " + ticketCost.toFixed(2);
costOut.innerHTML = "Amount due: " + finalCost.toFixed(2);
priceOut.innerHTML = "Price per ticket: " + ticketPrice.toFixed(2);
seatOut.innerHTML = "Your seating area is: " + seatingAreaCategory;
surchargeOut.innerHTML = "Card fee: " + surcharge.toFixed(2);
validQtyOut.innerHTML = "Number of tickets ordered: " + ticketQty;
}
window.onload = init;
<span id="beforeSurcharge"></span><br/>
<span id="amountDue"></span><br/>
<span id="pricePerTicket"></span><br/>
<span id="seatingArea"></span><br/>
<span id="surcharge"></span><br/>
<span id="ticketQty"></span><br/>
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报