ICE LECTURE In Class Exercises - Loops

Note: Please keep the programs that you create today, in case you have a question about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy.

ICE Part 1: 'Tracing' an accumulator program

Open up the file for the TraceThisAccumulatorPattern_ICE.html.  Your goal is to trace the program much like the instructor did in the slides.  You can do this on paper or on your computer (if you want to create a table in Microsoft Word open up the 'Insert' ribbon and then use the 'Table' button). 

HINT: If you want to check your answers you can add the following line into your program at the point where you want to check your work:

alert(outputString);
 

This will then cause the browser to display the value of outputString to you in an alert box (except for the newline characters (the \n characters), which will cause a line break that you can see in the alert box).

ICE Part 2: Using the for Statement

Write a script that uses a for loop to display the numbers from 1 to 20 down the left hand side of the document.

Alter the script to display the numbers from 1 to 100, 2 to 50 by twos, 50 down to 5 by fives, etc.

ICE Part 3: Generating a List Using a for Statement

Use a for loop to generate a single string that represents an html unordered list (using the <ul> and <li> tags), where each list element is a string entered by the user into a prompt box. The user should enter 5 items to put into the list.

You will need a string variable to serve as an accumulator. Each time through the loop, you should append the string entered by the user into the prompt to the end of the accumulator variable, surrounded by <li> and </li> tags. At the end, you should put <ul> and </ul> tags at either end of the string, with the following statement:

        accumulator = "<ul>" + accumulator + "</ul>";

If the user types in the word "END" then you should stop accumulating input from the user and display the unodered list on the page immediately.

If the user types in the word "SKIP" then you should ignore that input AND continue to accumulate input from the user.