1.12 Hand-in: Calculating loops

Loops that calculate

In Otto the Robot and the Ms. Pacman exercise you used loops to simply repeat the same thing many times.

In this assignment you will be creating loops that will calculate something useful.

For loop structure

for loops work like this:


	for (loopVariable = 0; loopVariable < 10; loopVariable = loopVariable +1) {
		/*	repeated thing goes in here */
		}
			

More generally, a for loop is:


	for (initial variable condition; cycle while true; variable change at end of cycle) {
		/*	repeated thing goes in here */
		}
			

Example code: multiples of three

Look at the following code

Focus questions

What do you expect will be the precise output?

What do you think thisMultiple.toString() does?

What is the difference between:

Multiples of three

Please enter the maximum multiple of three to list:

Saving your work

Download the template and save it as "1.12H-CalculatingLoops-LastName.html" in your Computer Programming 12 directory.

The assignment

You are going to be creating a program that does something similar to the Multiples of three example. Please create a web page that:

  1. Asks the user to input a number
  2. Uses some kind of event, like clicking a button, to trigger your Javascript code.
  3. Breaks down your code into separate functions following input/processing/output
  4. Uses loops to perform a calculation
  5. And outputs the results
  6. Has well-commented code with good style

Evaluation (out of 10)

Extend & expand

How can you do this with style?

Powers of two?

Progressive fractions: 1/1, 1/2, 1/3, 1/4...

How could you alter to code so that you could enter both the initial variable value, the increment, and the maximum multiple?

Can you use loops to generate a times table?