8.4  Java Script - Loops - while

Index Code Tester

OBJECTIVE

DISCUSSION

while Fundamentals 

while Statement

    
    while (condition) {
    [statements here]
    }
    

var i = 0; 
while (i <= 3) {  
    alert(i);
    i++;   //what if this statement is i--?
}; 
 

EXERCISES

1.  Write a while loop to display the numbers from 3 back to 1 in an alert one at a time.

2.  Write a while loop to write the numbers 1 to 100 in increasing font-sizes.

3.  Write a while loop to count down from 100 to 0 by two's while writing the numbers on a page using document.write. The page should display 100, 98, 96, ... , 0.

4.  Create a page that prompts the user for a number between 1 and 10.  Keep asking the user to enter a number until the number is between 1 and 10.  Use a prompt to request the number.  A prompt is like an alert but it returns what the user enters into it.  Test out the following statement in CodeTester: alert(prompt("Enter something"))

5.  Someone makes you the following offer. They will give you either (1) $1,000,000 or (2) the number of pennies that you would have if you start with one penny on day 1 and double the number of pennies every day for 30 days until you reach day 31.  You need to determine what is the better offer.  So that you can use the while loop you should write a program that determines the number of pennies that you will have each day from day 1until the day that the number of pennies exceeds 100,000,000 (which is $1,000,000).  Your program should assemble a table that lists the day number and the number of pennies.  Your table should look like the following except that it should stop when the number of pennies exceeds 100,000,000:

1    1
2    2
3    4
4    8
5   16

LINKS AND HELP

http://www.w3schools.com/js/js_looping.asp