为什么输出中的类型未定义?

时间:2014-10-12 04:13:40

标签: javascript function while-loop undefined

我运行代码后,在TYPE列下未定义。我看了我的功能,他们似乎很好。 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>

1 个答案:

答案 0 :(得分:1)

我相信你想要做的就是改变这一行:

displayOneOrder(countNumber, ticketName, quantity, charge, delta);

......对此:

displayOneOrder(countNumber, ticketType, quantity, charge, delta);

换句话说,当您打算通过ticketName时,您已为第二个参数传递ticketType

更新:或者,如果您希望它显示故障单类型的全名(例如,&#34;成人&#34;而不是&#34; A&#34;),则将该行更改为此:

displayOneOrder(countNumber, findTicketName(ticketType), quantity, charge, delta);

...或在其前面加上一行来设置ticketName的值,如下所示:

ticketName = findTicketName(ticketType);