OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JavaScript beginner: How to restart a loop

  • Thread starter Thread starter Scoob
  • Start date Start date
S

Scoob

Guest
Once the user guesses the right number I need to ask if the user would like to play again. As it is the loop will just repeat itself but what I need is the prompt box to ask if you would like to play again. If the user replies yes the loop will initiate again until the answer is guessed

Code:
<HTML>    
    <HEAD>    
    </HEAD>    
    <BODY
        <FORM NAME="testform">
            <BR>
            <BR>
            <BR>
        </FORM>

        <INPUT id="attempts" TYPE="text" NAME="inputbox" VALUE="" />
        <INPUT id="zero" TYPE="button" NAME="resetbox" VALUE="Reset " onclick="reset()" />

        <SCRIPT type="text/javascript">
            varattempts = 0;
            x = Math.round((Math.random()*19))+1;
            var tip; 

            tip=prompt("Do you want to play a game?")

            while(tip.charAt(0).toLowerCase() == "y")    
            {    
                var Guess;
                document.getElementById('attempts').value = 0;
                do 
                {    
                    Guess = prompt("Pick a number between 1 and 20","")
                    if (Guess === null) break;
                    document.getElementById('attempts').value = parseInt(document.getElementById('attempts').value)+1
                } while (Guess!=x);

                if (Guess == x)     
                {               
                    alert("You guessed right!")                 
                }
            }               
            function reset()                
            {
                varattempts=0;
                document.getElementById('attempts').value = 'Attempts: 0'; 
            }
        </SCRIPT>  
    </BODY>    
</HTML>

<p>Once the user guesses the right number I need to ask if the user would like to play again. As it is the loop will just repeat itself but what I need is the prompt box to ask if you would like to play again. If the user replies yes the loop will initiate again until the answer is guessed</p>

<pre><code><HTML>
<HEAD>
</HEAD>
<BODY
<FORM NAME="testform">
<BR>
<BR>
<BR>
</FORM>

<INPUT id="attempts" TYPE="text" NAME="inputbox" VALUE="" />
<INPUT id="zero" TYPE="button" NAME="resetbox" VALUE="Reset " onclick="reset()" />

<SCRIPT type="text/javascript">
varattempts = 0;
x = Math.round((Math.random()*19))+1;
var tip;

tip=prompt("Do you want to play a game?")

while(tip.charAt(0).toLowerCase() == "y")
{
var Guess;
document.getElementById('attempts').value = 0;
do
{
Guess = prompt("Pick a number between 1 and 20","")
if (Guess === null) break;
document.getElementById('attempts').value = parseInt(document.getElementById('attempts').value)+1
} while (Guess!=x);

if (Guess == x)
{
alert("You guessed right!")
}
}
function reset()
{
varattempts=0;
document.getElementById('attempts').value = 'Attempts: 0';
}
</SCRIPT>
</BODY>
</HTML>
</code></pre>
 

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top