0

Hi

Have a problem writing what i am sure is simple Javascript but have been trying to get round a problem for ages.

The program asks the user to enter either 1 or 2 (there is a window prompt to say what option 1 and 2 are)

When they enter 1 or 2 the program then displays a appropriate message on the screen.

If the user enter any other value a message appears telling them they have entered a wrong value and to try again.

So far this all works fine. What i am having trouble with is i want the program to bring up the window prompt with the first question again (please enter 1 or 2) this can be done with a simple loop but i only want the program to do the loop once. If the user enters a wrong value again i want the program to end.

If anyone can offer any help with this i would be very grateful. I am a newbie at Javascript so i hope it's not really simple!!!!!

Cheers

Matt

p.s. can post my code so far if it helps?

5
Contributors
9
Replies
10
Views
9 Years
Discussion Span
Last Post by n33712
0

This is the Java, not JavaScript, forum. You see the difference there?

Next time, post in the JavaScript forum, please.

I have already requested th admins move this thread.

0

Though I am not very clear as to what you mean, this snippet should give you a fair idea. If it doesn't, post your code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Example</title>
</head>
<body>
<script type="text/javascript">
    (function doSomething() 
    {
        while(true) {
            var ans = prompt("Enter either 1 or 2.");
            if(!ans || (ans != '1' && ans != '2')) {
                alert("You entered an invalid choice. Exitting...");
                break;
            }
            alert("You entered " + ans);
        }
    })();
</script>
</body>
</html>
0

My code:

var option;

option = window.prompt('Please enter 1 if you would like to join or 2 if you are an existing user',''); 
option = parseFloat(option);

if (option == 1)
{
  document.write('Welcome, we will need to take some details')  
}
else
{
    if (option == 2)
    {
      document.write('Enjoy')
    }
        else
    {
          if (option != 1,2);
             { 
               window.prompt('Please choose from the available options, Please try again!')  
             }   
        }
}

What i need is for the user if they entered anything other than 1 or 2 to be asked once more what option they would like. If they fail on the second attempt then the program closes.

Cheers

Matt

Edited by mike_2000_17: Fixed formatting

0

Hi,
I hope following is solution for u'r trouble,
(i have modify ~s.o.s~ code)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Example</title>
</head>
<body>
<script type="text/javascript">
 var loopTime=0;
    (function doSomething() 
    {
        while(true) {
            var ans = prompt("Enter either 1 or 2.");
            if(!ans || (ans != '1' && ans != '2')) {
                alert("You entered an invalid choice. Exitting...");
                if(loopTime>0)
                break;
                loopTime=1;
            }
            else
            {
                loopTime=0;
                alert("You entered " + ans);
            }

        }
    })();
</script>
</body>
</html>

Edited by mike_2000_17: Fixed formatting

0

Hi

Thanks for your help so far.

I'm sorry for the confusion but have just been told i am to do this without using a loop!
Am more confused now.

Any help appreciated

0

hi u dont want loop then try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Example</title>
</head>
<body>
<script type="text/javascript">
var count=0;
    (function recTest() 
    {
            var ans = prompt("Enter either 1 or 2.");
            if(!ans || (ans != '1' && ans != '2')) 
           {
                alert("You entered an invalid choice !");
                count++;				
            }
			else
			{
				alert("You entered " + ans);
				count=0;
			}
			if(count<=1)
				recTest();
	})();
</script>
</body>
</html>
0

HI

Thank you so much for your help.
What you have written above is almost exactly what i wanted. The only thing that i needed was a different message appearing on the screen depending on whether the user entered 1 or 2. I have been trying to come up with a answer on my own but as you may have guessed Javascript is not my thing :)
At the moment is gives a message saying "You entered (+ answer user typed)"
Is there a easy answer to getting a different message for answer "1" and answer "2"?

Cheers again for your help

Matt

0

Hi

Managed to get it all working now. Thank you for all your help.
Not sure i would have made it without your help.

Cheers

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.