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 " +fillingArray[fillingChoice] +" on " +breadArray[breadChoice] +" sandwich for lunch");
</script>
</head>
<body>
</body>
This will happen if the user enters
4when propmted forbreadChoice, sincebreadArray[4]is effectively undefined (the fourth and last element isbreadArray[3]).To prevent this from happening, you could use:
No, but it isn’t right nonetheless. Your
if-elsestatement will executealert("Thanks!...);in the first and second case, but in each only one condition gets validated. I suggest usingwhich will simply test if
breadArray[breadChoice]andfillingArray[fillingChoice]are defined.