JavaScript Homework Help: How do i fix my code so that it loops properly and give an error?
-
I need to set up a loop/debugging tool that allows the user to try again if they input the wrong data as well as puts out an alert letting them know they input invalid data. I'm a little stuck and need some help with my code! Here's the assignment details: 1. Use looping, and debugging tools as needed, to give the user an error message that is appropriate and the opportunity to enter the bread choice until they enter a valid bread choice. 2. Use looping, and debugging tools as needed, to give the user an error message that is appropriate and the opportunity to enter the filling choice until they enter a valid filling choice. 3. Once a valid bread choice and a valid filling choice have been entered display the message about what sandwich you will be ordering for them. & my code so far: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Assignment 7: Stephanie Hussar</title> <script type="text/javascript"> if (breadArray[breadChoice] && fillingArray[fillingChoice]) { alert("I will order you a " +fillingArray[fillingChoice] +" on " +breadArray[breadChoice] +" sandwich for lunch"); } else if(isNaN(breadChoice) || isNaN(fillingChoice)) { alert("The value you entered is not a number."); } else { alert("Sorry. You did not enter a correct numeric value, please try again!"); } </script> </head> <body> <script type="text/javascript"> var breadArray = new Array ("white","wheat","rye","wrap"); var fillingArray = new Array ("ham","turkey","egg salad","beef","peanut butter"); var breadChoice; var fillingChoice; var counter = 0; do { breadChoice = 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")-1; counter ++; } while(breadChoice >= 4 || breadChoice < 1); do { fillingChoice = 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")-1; counter ++; } while(fillingChoice >= 5 || breadChoice < 1); </script> </body>
-
Answer:
Here's an example. http://pastebin.com/VqwjLp8u Use the array literal instead of new. var myArray = ["element0", "element1"]; Instead of. var myArray = new Array("element0", "element1"); Anywho, hope this helps. - Dominic
dolceand... at Yahoo! Answers Visit the source
Related Q & A:
- How can I fix parser Error in ASP.NET?Best solution by parse.com
- How do I fix the "Specified path is too long" error?Best solution by itcsupport.wordpress.com
- Why am I receiving a malformed text data error and how do I fix it?Best solution by Yahoo! Answers
- How do I fix a Spooler subsystem app error?Best solution by Yahoo! Answers
- How do i fix the combat arms error?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.