[英]Why is the type undefined in my output?
運行代碼后,我在TYPE列下變得未定義。 我看了看我的功能,它們對我來說似乎還不錯。 TYPE列應為“兒童”,“初級”或“成人”。 您可以在提示中輸入10,以說明售出了多少張門票以及收費。
<script type="text/javascript">
var BR = "<br />";
var JUNIOR_PRICE = 7.50;
var ADULT_PRICE = 11.50;
function main()
{
var ticketType;
var quantity = 0;
var charge = 0.00;
var ticketName;
var currentDate = "";
var cAccum = 0;
var jAccum = 0;
var aAccum = 0;
var numTickets = 0;
var countNumber = 0;
var delta = 0.00;
var totRevenueC = 0.00;
var totRevenueJ = 0.00;
var totRevenueA = 0.00;
var totRevenue = 0.00;
var ticketsSold = 0.00;
document.write("SUMMERVILLE SEAWORLD" + BR);
currentDate = prompt("What is today's date? (ex: October 11, 2014)", "");
document.write("Summary report for " + currentDate + BR);
ticketType = getTicketType();
while (ticketType != 'Q')
{
quantity = getQuantity();
charge = getCharge();
if (ticketType == "C")
{
cAccum += quantity;
delta = 0;
totRevenueC = 0;
}
else if (ticketType == "J")
{
jAccum += quantity;
delta = charge - (quantity * JUNIOR_PRICE);
totRevenueJ = (jAccum * JUNIOR_PRICE) - delta;
}
else
{
aAccum += quantity;
delta = charge - (quantity * ADULT_PRICE);
totRevenueA = (aAccum * ADULT_PRICE) - delta;
}
countNumber ++;
ticketsSold += quantity;
totRevenue = totRevenueC + totRevenueJ + totRevenueA;
ticketType = getTicketType();
displayOneOrder(countNumber, ticketName, quantity, charge, delta);
}
displayFinalReport(currentDate, countNumber, aAccum, totRevenueA, jAccum, totRevenueJ, cAccum, totRevenueC, totRevenue);
}
function getTicketType()
{
var tickType;
tickType = prompt("Enter ticket type: C, J, A or Q to quit", "");
tickType = tickType.toUpperCase();
while (tickType != "C" && tickType != "J" && tickType != "A" && tickType != "Q")
{
tickType = prompt("Invalid entry. Enter ticket: C, J, A or Q to quit", "");
tickType = tickType.toUpperCase();
}
return tickType;
}
function getQuantity()
{
var qty;
qty = prompt("How many tickets sold?", "");
qty = parseFloat(qty);
//Validation loop for a negative amount
while (qty < 0.0)
{
qty = prompt ("Negative sale values are not allowed! Please enter the sale for that region:", "");
qty = parseInt(qty);
}
return qty;
}
function getCharge()
{
var chrg;
chrg = prompt("What is the charge?", "");
chrg = parseFloat(chrg);
//Validation loop for a negative amount
while (chrg < 0.0)
{
chrg = prompt ("Negative sale values are not allowed! Please enter the sale for that region:", "");
chrg = parseFloat(chrg);
}
return chrg;
}
function findTicketName(tickType)
{
var tickName;
if (tickType == "C")
{
tickName = "Child";
}
else if (tickType == "J")
{
tickName = "Junior";
}
else
{
tickName = "Adult";
}
return tickName;
}
function displayOneOrder(count, tickName, qty, chrg, delt)
{
document.writeln("<pre>");
document.writeln("COUNTER #" + " " + "TYPE" + " " + "QTY" + " " + "ACTUAL CHG" + " " + "DELTA");
document.writeln(count + " " + tickName + " " + qty + " " + chrg.toFixed(2) + " " + delt.toFixed(2) + BR);
document.writeln("</pre>");
}
function displayFinalReport(currDate, count, aduAccum, totRevA, junAccum, totRevJ, chiAccum, totRevC, totRev)
{
document.write("SUMMERVILLE SEAWORLD" + BR + BR);
document.write("Summary report for " + currDate + BR + BR);
document.write("Number of records: " + count + BR + BR);
document.write("SALES TOTALS");
document.writeln("<pre>");
document.writeln("TICKET TYPE" + " " + "TOTAL TICKETS" + " " + "TOTAL REVENUE");
document.writeln("ADULT" + " " + aduAccum + " " + totRevA.toFixed(2) + BR);
document.writeln("JUNIOR" + " " + junAccum + " " + totRevJ.toFixed(2) + BR);
document.writeln("CHILD" + " " + chiAccum + " " + totRevC.toFixed(2) + BR + BR);
document.writeln("TOTAL REVENUE: " + totRev.toFixed(2));
document.writeln("</pre>");
}
</script>
我相信您要做的就是更改此行:
displayOneOrder(countNumber, ticketName, quantity, charge, delta);
...對此:
displayOneOrder(countNumber, ticketType, quantity, charge, delta);
換句話說,你通過ticketName
對於第二個參數,當你的意思是通過ticketType
代替。
更新:或者,如果您希望它顯示票證類型的全名(例如,“成人”而不是“ A”),則可以將該行更改為此:
displayOneOrder(countNumber, findTicketName(ticketType), quantity, charge, delta);
...或在其前面加上一行以設置ticketName
的值,如下所示:
ticketName = findTicketName(ticketType);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.