I am making a game with JavaScript and it is just prompt after prompt asking you questions to make decisions, such as: -
var turn=prompt("Will you turn left or right?");
-
if (turn="left") {
-
document.write("You chose to turn left!");
-
window.location="nextchoice.html";
-
}
-
if (turn="right") {
-
document.write("You chose to turn right!");
-
window.location="nextchoice.html";
-
}
-
How can I reset the prompt if the user hasnt put one of the answer choices he had to?
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. -
var turn;
-
-
do
-
{
-
// prompt
-
} while(turn isn't "left" and turn isn't "right");
-
-
// do something with turn
-
To correctly fill in the while statement, you'll need to read up on using logical expressions.
6 1550
Put it in a loop until a satisfactory answer has been given. In psedocode it would be like this: - loop until input equals left or right
-
input = ask user to input left or right
-
end loop
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!
-
if (turn="left")
-
...
-
if (turn="right")
-
...
-
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 =
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"
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. -
var turn;
-
-
do
-
{
-
// prompt
-
} while(turn isn't "left" and turn isn't "right");
-
-
// do something with turn
-
To correctly fill in the while statement, you'll need to read up on using logical expressions.
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... -
<!DOCTYPE html>
-
<html lang="en">
-
<head>
-
<meta charset="utf-8" />
-
<title> Untitled </title>
-
</head>
-
<body>
-
<div id="debugger"></div>
-
-
<script type="text/javascript">
-
var turn;
-
do {
-
turn = prompt('',"Will you turn left or right?");
-
if (turn=="left") {
-
document.getElementById('debugger').innerHTML = "You chose to turn left!";
-
// window.location="nextchoice.html";
-
}
-
if (turn=="right") {
-
document.getElementById('debugger').innerHTML = "You chose to turn right!";
-
// window.location="nextchoice.html";
-
}
-
} while ( (turn == 'right') || (turn == 'left') );
-
-
</script>
-
-
</body>
-
</html>
-
Note also that window.location will cause the page to close and open the 'nextchoice.html' site.
Sign in to post your reply or Sign up for a free account.
Similar topics
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) {...
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |