0
我在運行我的代碼後,在TYPE列下出現undefined。我看着我的功能,他們對我來說似乎很好。 TYPE列應該是Child,Junior或Adult。您可以在提示中輸入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>
因爲你傳遞一個打印它'ticketName'變量,它從來沒有分配給它的值的功能。 – 2014-10-12 04:23:42
這就是我在函數findTicketName中要做的。它從函數getTicketType接收tickType。然後我想讓tickName在函數displayOneOrder中顯示它。 – jaramore 2014-10-12 04:33:21
findTicketName永遠不會在代碼中的任何位置調用,這就是它未被分配的原因。 – 2014-10-12 04:41:35