DMS102/CSC170D: Introduction to Web Development
Spring 2013
Assignment 5: Iteration
- [2 pts]
Create a page that displays the square of each of the first ten
integers starting at 1 using a table.
- [2 pts]
Create a page that asks the user for a number and then displays
the square of the ten numbers starting with that one, using a table.
- [2 pts]
Create a page that uses a
for loop to demonstrate
each of the six possible HTML heading types
(h1..h6).
- [2 pts]
Create a page that asks the user for a number n and then
uses a
for loop to compute the value of n!
(the factorial
function). I'll save you the trouble of looking it up: n!
is the product of all the numbers from 1 up to and including n.
- [2 pts]
Create a page that asks the user for a year (number) and displays
whether or not it is a leap year. Use only one
if statement
with a Boolean expression to do the test. The definition of a leap
year is a year that is evenly divisible by 4, but not evenly
divisible by 100, unless also evenly divisible by 400.
- [4 pts]
Create a page that helps with grading as follows:
-
Ask the user for the name of a student. If there are no more
students, the user can enter "stop". (Make this clear in your
prompt.)
-
For that student, ask the user to enter the student's grades
one at a time. If there are no more grades, the user can enter
"stop".
-
Display the following as a row in a table:
- The student's average grade
- The student's maximum grade
- The student's minimum grade
Make sure your table has informative column headers, and
feel free to style the table to look better if you like.
-
Repeat until the user enters "stop" for a student name. (Think
about how you need to structure your loops to achieve this.)
Note: Think about how/when you need to start and end your table.
Things to think about
-
Iteration allows you to repeat a statement or block. Combined
with conditionals, you can do different things each time around
the loop.
-
It is crucial that the body of a loop change something that
(eventually) changes the value of the loop condition from true to
false, otherwise the loop will never stop. See what happens when
your browser executes a script with an infinite loop (hint: it
ain't pretty).
- There are many ways to write iterative programs:
-
The book describes counter-controlled and sentinel-controlled
loops, which are different ways of deciding when to end the
iteration;
-
while, for,
and do-while loops, which are
different control statements for formulating your iteration in
Javascript.
Deciding how to express your algorithm using the various control
constructs is part of the art of programming. But you should be
familiar with all the tools in the toolkit so you can make the
right choices when designing your program.
- Iteration control constructs can be nested, just like HTML
elements and conditionals. Makes you think that nesting (embedding)
is kind of an important concept, no?
-
Iteration is particularly nice for generating tables in web pages,
no?
Last update: Thursday, 07-Feb-2013 11:08:24 EST