473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How can I reset prompt if the user hasn't put one of the answer choices he had to?

I am making a game with JavaScript and it is just prompt after prompt asking you questions to make decisions, such as:
Expand|Select|Wrap|Line Numbers
  1. var turn=prompt("Will you turn left or right?");
  2. if (turn="left") {
  3. document.write("You chose to turn left!");
  4. window.location="nextchoice.html";
  5. }
  6. if (turn="right") {
  7. document.write("You chose to turn right!");
  8. window.location="nextchoice.html";
  9. }
  10.  
How can I reset the prompt if the user hasnt put one of the answer choices he had to?
Mar 14 '13 #1

✓ answered by divideby0

I was using your code, albeit in an abreviated form, to let you know in case it wasn't just a typo. :)

Rabbit provided the answer, so I thought it would be best to leave that part alone. If you still need a slight nudge, then my preference would be to use a do while loop.

Expand|Select|Wrap|Line Numbers
  1. var turn;
  2.  
  3. do
  4. {
  5.    // prompt
  6. } while(turn isn't "left" and turn isn't "right");
  7.  
  8. // do something with turn
  9.  
To correctly fill in the while statement, you'll need to read up on using logical expressions.

6 1550
Rabbit
12,516 Expert Mod 8TB
Put it in a loop until a satisfactory answer has been given. In psedocode it would be like this:
Expand|Select|Wrap|Line Numbers
  1. loop until input equals left or right
  2.    input = ask user to input left or right
  3. end loop
Mar 14 '13 #2
Wow Rabbit, you are extremely helpful! No wonder you are on the top contributors list! However, I have a question: I am 12 and am on a JavaScript course, and my next unit is Loops. However, I really want to start making this game right now, and I don't want to bother you, but can you please show me how to make a real loop in JavaScript and not in psedocode, meaning something the computer will understand. Thank you!
Mar 14 '13 #3
divideby0
131 128KB
Expand|Select|Wrap|Line Numbers
  1. if (turn="left")
  2.     ...
  3. if (turn="right")
  4.     ...
  5.  
Unrelated to the loop question, but the above code is assignment and not comparison. If you're wanting to test whether or not turn is equal to "left" or "right," then use == and not =
Mar 14 '13 #4
Thank you, @divideby0 , about the fact that you noticed that I had put assignment and not comparison operators. However, isn't the code you showed just another if/else statement? My question was if, say, in the prompt the user wrote "forward" which is not a valid answer, how can I make it so that the script returns to the same prompt and doesn't let the user go forward until he writes one of the valid answers, which are "left" and "right"
Mar 14 '13 #5
divideby0
131 128KB
I was using your code, albeit in an abreviated form, to let you know in case it wasn't just a typo. :)

Rabbit provided the answer, so I thought it would be best to leave that part alone. If you still need a slight nudge, then my preference would be to use a do while loop.

Expand|Select|Wrap|Line Numbers
  1. var turn;
  2.  
  3. do
  4. {
  5.    // prompt
  6. } while(turn isn't "left" and turn isn't "right");
  7.  
  8. // do something with turn
  9.  
To correctly fill in the while statement, you'll need to read up on using logical expressions.
Mar 14 '13 #6
jmrker
17
You cannot use a document.write() AFTER the page has loaded.
Doing so causes the page to reload and re-start the game.

Consider this...
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title> Untitled </title>
  6. </head>
  7. <body>
  8. <div id="debugger"></div>
  9.  
  10. <script type="text/javascript">
  11. var turn;
  12. do {
  13.   turn = prompt('',"Will you turn left or right?");
  14.   if (turn=="left") {
  15.     document.getElementById('debugger').innerHTML = "You chose to turn left!";
  16. //    window.location="nextchoice.html";
  17.   }
  18.   if (turn=="right") {
  19.     document.getElementById('debugger').innerHTML = "You chose to turn right!";
  20. //    window.location="nextchoice.html";
  21.   }
  22. } while ( (turn == 'right') || (turn == 'left') );
  23.  
  24. </script>
  25.  
  26. </body>
  27. </html>
  28.  
Note also that window.location will cause the page to close and open the 'nextchoice.html' site.
Mar 18 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Ken | last post by:
How can I reset the initial form variables that are set with session statements when clicking on a button? I tried this but the function was not called: <?PHP function reset_form($none) {...
3
by: Matthew | last post by:
I am have written a medium sized program and everything works fine except for one piece. At a certain point in the program I want to do a simple user confirmation, eg Are you sure ? y/n. The...
3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
1
by: jel | last post by:
I ran what was submitted in several forums, but it's not exactly what i'm looking for. I'm dy'n over here. Ah, the frustrations of an amateur programmer. I included the code below in c++. which...
1
by: PeteCresswell | last post by:
I'm working on a new report in an MS Access DB. The first anomaly was a message "This action will reset the current code in break mode." when the report was run. Seems TB something about my...
2
by: Andre L via AccessMonster.com | last post by:
We run numerous Access reports which we auto export as .rtfs to a share. We also have numerous Excel sheets. Each month 10 or more of these reports are combined into deprtmental monthly packets. ...
1
by: Troy | last post by:
How do I answer the disable macro prompt automatically when opening an Excel workbook? We are using Excel 97 and 2002. In 2002 we want the Medium security (prompt user) to safeguard against e-mail...
9
by: Rhino | last post by:
How hard (and desireable) would it be to give the user certain choices when it comes to printing web pages? The pages on my site use colours and pictures and contain an imbedded menu, among...
1
by: Oenone | last post by:
We have a little piece of JavaScript in one of our client's application which automatically sets the input focus to the first control on the HTML form. This works very nicely, except for in one...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.