简体   繁体   中英

JavaScript - What am I doing wrong?

Yet again another assignment I need help on. All i need is just the work checked over and tell me why the items in the first array aren't working properly to the corresponding number the user enters.

Directions:

Create a new file named a5_yourname.html. Use the steps below to write a script that will prompt the user for two numbers. One will give the type of bread chosen the other the type of filling chosen for a sandwich.

Use these choices to display a statement that will extract the correct name of bread and filling from the perspective arrays to send an alert message to the user. Use the variable names and messages as they are described in the steps.

A. Create an array named “breadArray”. Enter the following values into the breadArray: white, wheat, rye, wrap

B. Create a second array named “fillingArray”. Enter the following values in the fillingArray: ham, turkey, egg salad, beef, peanut butter

C. Use a prompt to tell the user what types of bread there are, giving them each a number and ask them to choose bread by the number.

Like: Bread choices are: 1 for white, 2 for wheat, 3 for rye and 4 for a wrap. Please select the type of bread for your sandwich using a number from 1 to 4.

D. Use an “if”- “else” statement and the “isNaN” function to determine if the user entered a number. Test if the number are within range. If they did not enter a valid number display an alert message box asking them to enter a number. Repeat this step after step “F” as well.

E. Save the number given in a variable named “breadChoice”.

F. Use a prompt to tell the user what types of fillings there are, giving them each a number and ask them to choose a filling by the number. Like: Filling choices are: 1 for ham, 2 for turkey, 3 egg salad, 4 beef, and 5 peanut butter Please select the type of filling for your sandwich using a number from 1 to 5.

G. Save the number given in a variable named “fillingChoice”.

H. If the user entered a valid number then use the numbers given by the user to extract the correct bread and filling, from the appropriate array, for the sandwich and display the following message using the document.write statement:

“I will order you a “name of filling” on “name of bread” sandwich for lunch.” Example: If the user enters 1 for bread and a 2 for filling then the message should read: I will order you a ham on wheat sandwich for lunch.

My Code:

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Assignment 5: Arrays</title>

    <script type="text/javascript">

        // Variable Declarations

            var breadArray = new Array ("white","wheat","rye","wrap");
            var fillingArray = new Array ("ham","turkey","egg salad","beef","peanut butter")
            var breadChoice;
            var fillingChoice;

        // Assignments

            breadChoice = Number(prompt("Bread choices are: 1 for White, 2 for Wheat, 3 for Rye, or 4 for a Wrap. Please select the type of bread for you sandwich using a number from 1 to 4"),0)-1;
            fillingChoice = Number(prompt("Filling choices are: 1 for ham, 2 for turkey, 3 egg salad, 4 beef, or 5 peanut butter. Please select the type of filling for your sandwich using a number from 1 to 5"),0)-1;

        // Calculations

            // none


        // Output


        if (breadArray[breadChoice] && fillingArray[fillingChoice])
        {
         alert("Thanks! I am now calculating your sandwich order.");
        }
        else
        { 
            alert("Sorry. You did not enter a correct numeric value, please try again!");
        }



        document.write("I will order you a &nbsp;" +fillingArray[fillingChoice] +"&nbsp; on &nbsp;" +breadArray[breadChoice] +"&nbsp; sandwich for lunch");

    </script>

</head>

<body>

</body>

Javascript中的数组(像许多但不是全部的编程语言一样)从索引0而不是1开始对第一个元素编号。

数组从0开始,而不是1。因此,如果我为白面包输入1,则需要从中减去1,以在数组中获得正确的位置:

document.write("I will order you a &nbsp;" +fillingArray[fillingChoice-1] +"&nbsp; on &nbsp;" +breadArray[breadChoice-1] +"&nbsp; sandwich for lunch");

the error is that it sometimes says undefined for the filling choice when i enter a valid number.

This will happen if the user enters 4 when propmted for breadChoice , since breadArray[4] is effectively undefined (the fourth and last element is breadArray[3] ).

To prevent this from happening, you could use:

breadChoice = Number(...) - 1;

I think the problem is my if, else statement actually.

No, but it isn't right nonetheless. Your if-else statement will execute alert("Thanks!...); in the first and second case, but in each only one condition gets validated. I suggest using

if (breadArray[breadChoice] && fillingArray[fillingChoice])
    alert("Thanks! I am now calculating your sandwich order.");
else 
    alert("Sorry. You did not enter a correct numeric value, please try again!");

which will simply test if breadArray[breadChoice] and fillingArray[fillingChoice] are defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM