CSC 110 Assignment 5
Looping Statements |
Due Date
Objectives
- Use a loop to perform repetitive tasks.
- Create a table in JavaScript.
- Format output in JavaScript.
- Fill in a table in JavaScript using loops.
- Use nested loops (optional).
Instructions
This assignment provides our first opportunity to use loops. The assignment
starts out with some simple output. Then it gets a bit more "interesting"
...
Simple Loops
For the first section, you will create some simple loops.
These loops will just count. Here are the specifications for this section:
- Create this section within one script tag.
- Within this script tag, write JavaScript that prompts the
user for a sentence (a line of text). The script should then print out
the sentence that the user gave.
- The script will also count the number of spaces in the
sentence. Use a while loop for this. The script will print out
the number of spaces in the sentence.
- Here is a sample:
Your sentence is:
Now is the time for all good people to come to the party.
There are 12 spaces in this sentence.
- [Hint] Remember the loop that we have seen several times in lab.
var
word = window.prompt("Enter a word", "");
document.write("<ol>");
for(var j = 0; j < word.length; j ++) {
document.write("<li>"+ word.charAt(j));
}
document.write("</ol>");
- Within the same script tag, write JavaScript to count to a user
specified limit.
- Prompt the user for a small positive integer.
- Verify that the value is between 5 and 75.
- Echo (write out) the value on the HTML page.
- Use a loop to count from 1 to the number supplied by the user.
- Remember to output the values with separators, for example
spaces, in a paragraph.
- Use a for loop.
- Extra credit option possible here. See below.
- For example:
Your number is 12.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
You can test your page at this point.
Mathematical Table
For the second section, we are going to use
a JavaScript loop to create data for a table. The output, a simple table,
will look like the following:

Here are some notes:
- Just like the Lab 7 exercise, part of this will be within the loop, part
will be outside (before or after).
var
word = window.prompt("Enter a word", "");
document.write("<ol>");
for(var j = 0; j < word.length; j ++) {
document.write("<li>"+ word.charAt(j));
}
document.write("</ol>");
- Within tables, you can create "heading cells" using <th> </th> instead
of <td> </td>. These heading cells will have bold centered text by default.
- The square of a number is the number times itself (n
* n). The cube is the number times itself times itself. You can use
the Math.sqrt method to calculate the square root. You're on your own for
2n, n/2 and 1/n.
- Notice there is no formatting in the <td>s. (There's an extra credit
option to make it look better.)
- Create this table completely within JavaScript.
- Use a different script tag than for part 1.
You're done with the coding part of the core assignment.
Comments and Formatting
So far, we haven't had much in the way of commenting and
formatting requirements. Now, we do.
- In the <head> for the page, include an HTML comment that
includes your name and this assignment number (4).
- In the JavaScript sections, use comments to indicate which part
of the problem you are addressing. For example for the while loop, you can
write
// This code is for part I
number 2.
Alternately, you could write
// This code uses a while loop
to count from 1 to 10.
- Make sure you label each part of the code. This includes any
extra credit options you attempt.
- It is very reasonable to have additional comments explaining
what you're doing. Especially if something seems a bit tricky as you're
working on it.
- Make sure that your code is cleanly indented. That is, all of
the statements within a pair of braces (the then-part or else-part of an if
statement, the "insides" of a loop) should be indented 2 - 4 spaces from the
left margin of the loop. They should line up vertically, as well.
Do not neglect the formatting and comments.
[Optional] Extra-Credit Exercises
Here are some more challenging exercises you can complete for extra
credit.:
- Supporting Negative Input [0.2 points]
For part 1, number 4, allow the user to put in negative values. So, valid
input values will range from -75 to -5 and from +5 to +75. If a negative
value is given, the loop should count down to the given value, rather than
up.
- Formatted Output [0.4 points]
For part 2, clean up the output formatting for the table. It should look
like this.

Notes:
- To get the √ symbol, use √
- Notice the alignment within the cells.
- There is a method of Number that will allow you to specify the
number of digits after the decimal point.
- Labeled Times Table [0.3 points]
In class, we made a table like this:

Update the times table
by adding labels (column and row headings) and by aligning the cell contents
appropriately.

For complete extra credit for this optional exercise, all of the numerical
output within this table should be written using looping
statements.
- Form-Based Input [0.3 points]
The user input in part 1 (Simple
Loops) uses window.prompt. Create two forms for the user input: one
for the "sentence" and one for the "number". Use document.getElementById
and innerHTML to display the results.
I recommend that you use a division <div> </div> with the
id attribute for the output.
Make sure you complete the core assignment before moving on to any enhancements.
If the core assignment is not complete (that is, if you did not demonstrate the
educational objectives listed at the beginning of the assignment), you cannot receive
any extra credit points.
Similarly, you cannot receive extra-credit points for assignments that are turned
in late. So, do not hang on to the assignment trying to wrest out maximum extra-credit
points. If the assignment is turned in after the due date and time, extra-credit work
will be ignored.
The maximum extra credit points available for this assignment is 1.0.
E-mail Assignment File
Assignments will be submitted using e-mail.
- Subject line: CSC 110 Assignment
- Body, required contents:
- Your full name
- This assignment number (five)
- List any extra-credit portions you want graded
- Attachment: The HTML file you created.
- Email to:
djinguji@sccd.ctc.edu
Schedule Page