Lab: A Javascript Program

CS 102

Feb. 12, 2001

The purpose of this lab is to look at a program written in a simple programming language --- Javascript --- to see what a program looks like and to develop a few skills which will come in handy when we take a look at spreadsheets next. After completing the lab, I expect that you would be able to read and understand similar short programs.

  1. Look over the following program and try to predict what it will do.
    <html>
    <head>
    </head>
    <body>
    <script><!--
    // This is a comment.  See the comment.  Run, comment, run.  :-)
    
    // Variable declarations.  Variables must be declared before
    // being used.
    var name;
    var age;
    var i;
    var currentYear = 1997;
    var birthYear;
    var response;
    
    // Some straight-line execution.
    name = prompt("What is your name?", "");
    document.write("Hello " + name + "!<br><br>");
    
    age = prompt(name + ", how old are you?", "");
    
    // A conditional.
    if (age >= 30)
       document.write("We can't trust you!<br><br>");
    else
       document.write("I wish I were young again!<br><br>");
    
    birthYear = currentYear - age - 1;
    
    response = prompt("Were your born in " + birthYear + "? (y or n)", "");
    if (response == "y")
       document.write("I'm smart, aren't I?<br><br>");
    else
       document.write("You were born too early in the year!<br><br>");
    
    // A loop.
    for (i = 1; i <= age; i = i + 1)
        document.write(i + "<br>");
    
    document.write("<br>Goodbye " + name + "!<br>");
    
    // You need the following comment in your program.
    //-->
    </script>
    </body>
    </html>
    

  2. There is a copy of the program available on the S: drive. Open the S: drive and your G: drive and drag a copy of the file jscript.htm from the S: drive to your G: drive.

  3. Open Notepad (Click the Start menu, Choose Programs, Choose Accessories, and choose Notepad. Open the copy of jscript.htm you just saved on your G: drive.

  4. Correct the line that assigns a year value to the variable currentYear. Can you explain why this change needs to be made in terms of the statements later in the program which use the variable?

  5. Open the page in the browser to run the program. To do this, open Internet Explorer, open the File menu, choose Open..., and then Browse to your copy of jscript.htm and open it. Your program will now run.

    You can use the Refresh button on the browser to rerun your program.

  6. Did the program do what you expected it to? If not, to increase your understanding, try to reconcile your predicted behavior of the program with what it actually did.

  7. Using Notepad, change the document.write statement near the beginning of the program that writes Hello name so that the output line is printed in italics. Insert the <i> </i> (begin and end italics) HTML tags at the appropriate points in the line to accomplish this. Save and re-run your program.

  8. Change the for loop near the end of the program so that it counts up by two (1, 3, 5, etc.) rather than one (1, 2, 3, etc.).



Thomas P. Kelliher
Sun Feb 11 10:33:23 EST 2001
Tom Kelliher