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.
<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>
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.
Notepad
. Open the copy of
jscript.htm
you just saved on your G:
drive.
currentYear
. Can you explain why this change needs to be made
in terms of the statements later in the program which use the variable?
jscript.htm
and open it. Your
program will now run.
You can use the Refresh button on the browser to rerun your program.
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.
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.).