Home

How to Use JavaScript to Make a While Loop for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:15:25
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

Here, you can see a dialog box asking for a password. When programming your HTML5 web page, you can use JavaScript to achieve this same box with a while loop. The program keeps asking for a password until the user enters the correct password.

image0.jpg

Here's the HTML code used for two different while examples:

<body>
 <h1>While Loop Demo</h1>
 <p>The password is 'HTML5'</p>
 <form action = ">
 <fieldset>
  <button type = "button"
    onclick = "getPassword()">
  guess the password
  </button>
  <button type = "button"
    onclick = "threeTries()">
  guess the password in three tries
  </button>
 </fieldset>
 </form>
</body>

The first version keeps popping up a dialog box until the user gets the answer correct.

 function getPassword(){
  //from while.html
  var correct = "HTML5";
  var guess = ";
  while (guess != correct){
  guess = prompt("Password?");
  } // end while
  alert("You may proceed");
 } // end getPassword

A while loop for passwords is not hard to build:

  1. Store the correct password in a variable.

    Variable names are important because they can make your code easier to follow. Beginners often call one of these variables pass-word, but that can be confusing because there are actually two passwords (the correct password and the guessed password) in play here.

    The best way to design variable names is to anticipate the conditions they will be used in. This function is based on the condition guess==correct. This is a really nice condition because it's really easy to determine what we're trying to figure out (whether the guess is correct). It takes some practice to anticipate variable names well, but it's a habit well worth forming.

  2. Initialize the guess to an empty value.

    The key variable for this loop is guess. It starts as an empty string. It's critical to initialize the key variable before the loop begins.

  3. Set up the while statement.

    The while statement has extremely simple syntax: the keyword while followed by a condition, followed by a block of code.

  4. Build the condition.

    The condition is the heart of a while loop. The condition must be constructed so the loop happens at least once (ensure this by comparing the condition to the variable initialization). When the condition is true, the loop continues. When the condition is evaluated to false, the loop will exit. This condition compares guess to correct. If guess is not equal to correct, the code will continue.

  5. Write the code block.

    Use braces and indentation to indicate the block of code that will be repeated in the loop. The only code in this particular loop asks the user for a password.

  6. Add code to change the key variable inside the loop.

    Somewhere inside the loop, you need code that changes the value of the key variable. In this example, the prompt statement changes the password. As long as the user eventually gets the right password, the loop ends.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].