Programming with JavaScript

CS 102

Feb. 12, 1999

  1. Look over the following program and try to predict what it will do.

    <html>
    <head>
    </head>
    <body>
    <script><!--
    // This is a comment.  You don't need to type-in any of these, except
    // for the one at the end.
    
    // Variable declarations.
    var name;
    var age;
    var i;
    var date = new Date();
    var currentYear
    var birthYear;
    var response;
    
    // Compute the correct current year.
    currentYear = date.getYear();
    if (currentYear < 1000)
       currentYear = currentYear + 1900;
    
    // 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. Run the program: http://phoenix.goucher.edu/~kelliher/cs102/javascript.html .

  3. Did it do what you expected it to? Try to reconcile your predicted behavior of the program with what it actually did.

  4. From Internet Explorer, show me how to view the source code for the web page.



Thomas P. Kelliher
Fri Feb 12 09:40:22 EST 1999
Tom Kelliher