CSC 110 Assignment 5
Looping Statements

Due Date

Objectives

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:

  1. Create this section within one script tag.
  2. 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.
  3. 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.
  4. 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.
  5. [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>");
  6. Within the same script tag, write JavaScript to count to a user specified limit.
    1. Prompt the user for a small positive integer.
    2. Verify that the value is between 5 and 75.
    3. Echo (write out) the value on the HTML page.
    4. Use a loop to count from 1 to the number supplied by the user.
    5. Remember to output the values with separators, for example spaces, in a paragraph.
    6. Use a for loop.
    7. Extra credit option possible here. See below.
    8. 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:

  1. 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>");
  2. Within tables, you can create "heading cells" using <th> </th> instead of <td> </td>. These heading cells will have bold centered text by default.
  3. 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.
  4. Notice there is no formatting in the <td>s. (There's an extra credit option to make it look better.)
  5. Create this table completely within JavaScript.
  6. 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.

  1. In the <head> for the page, include an HTML comment that includes your name and this assignment number (4).
  2. 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.
  3. Make sure you label each part of the code. This includes any extra credit options you attempt.
  4. 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.
  5. 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.:

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.

Schedule Page