473,387 Members | 1,575 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,387 software developers and data experts.

Should I use a while or a do while loop if I want a prompt to loop till valid answer?

I have previously said that I am making a game that is text based with javascript, and I got a bunch of helpful replies, which told me to use a loop, but I do not know what loop exactly to use and how. Here is the context that I am using it in:
Expand|Select|Wrap|Line Numbers
  1. var turn=prompt("Where do you want to turn? Left or right?");
  2. if (turn=="left"){
  3. window.alert("You chose to turn left!");
  4. window.location="somepage1.html";
  5. }
  6. if (turn=="right"){
  7. window.alert("You chose to turn right!");
  8. window.location="somepage2.html";
  9.  
Please help. Thanks in advance.
Mar 16 '13 #1

✓ answered by divideby0

The type of loop depends on where you need/want to test. The while loop tests at the top, so the loop body may or may not be entered depending on the test condition. The do while tests at the bottom and will enter the loop body at least once. The for loop can behave either way depending on how it's initialized.

With your code, maybe something like

Expand|Select|Wrap|Line Numbers
  1. var turn = prompt(...);
  2.  
  3. while(test conditions are false)
  4. {
  5.    turn = prompt(...);
  6. }
  7.  
  8. // manipulate turn's value here
  9.  
Another thing is javascript treats LEFT, left, LEft, etc as different strings. To deal with that, you can use turn.toLowerCase() or turn.toUpperCase() in your test conditions.

1 1286
divideby0
131 128KB
The type of loop depends on where you need/want to test. The while loop tests at the top, so the loop body may or may not be entered depending on the test condition. The do while tests at the bottom and will enter the loop body at least once. The for loop can behave either way depending on how it's initialized.

With your code, maybe something like

Expand|Select|Wrap|Line Numbers
  1. var turn = prompt(...);
  2.  
  3. while(test conditions are false)
  4. {
  5.    turn = prompt(...);
  6. }
  7.  
  8. // manipulate turn's value here
  9.  
Another thing is javascript treats LEFT, left, LEft, etc as different strings. To deal with that, you can use turn.toLowerCase() or turn.toUpperCase() in your test conditions.
Mar 16 '13 #2

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

Similar topics

3
by: addi | last post by:
All, I will be eternally greatful if someone can provide snippet of code, URL or reference material that shows how to display data in a "n colums * n rows" format. I am new to ASP and have...
1
by: shivers3000 | last post by:
Can someone explain why $displayResults will not query twice? Scripts Objective: to take a car's VIN retrieve the car info and display in nice rows Issue with Script: The while loop displays...
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...
5
by: rjames.clarke | last post by:
I have the following. $result=mysql_query($sql); $nrows=mysql_num_rows($result); for ($i=0;$i<$nrows;$i++) { $row_array=mysql_fetch_row($result); echo "<form name='testform'...
3
by: Rudy | last post by:
I am writing a program in VB.NET and as I was debugging a problem I noticed my For loop doesn't want to loop! I originally had a upper bound which was an expression and it wasn't working. WHen I...
1
by: David | last post by:
Hi, I'm having trouble copying table data to new records. I have two tables as follows: *** Specifications (Table) specification_ID (field) LINKED product_ID (field) specification_header...
7
by: itfetish | last post by:
Ok heres my problem, I have a database which has a table in it that has all the staff members who are currently signed out of hte office. That has their staff ID, and the time they are out till...
23
by: Sacred Heart | last post by:
Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My...
17
by: takayuki | last post by:
Hi, I'm studying python via the exellent book "How to think like a python programmer" by Allen Downey. Noob question follows... animals.txt is a list of animals, each on a separate line:...
6
by: nickels | last post by:
Ok i already made a program that uses char and when someone enters a letter it will give them a conversion i made for that letter. That looks like this. import java.util.Scanner; public class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.