I don't know what you're using as a debugging tool, but there are minor
problems you need to resolve ...
> var stToday = new Date.getDate();
Not sure what you're trying for here, it's a bit confused. "st"
suggests you are going for a string, but you try to give it a "new" and
you try to use it as a date in the next line. However, getDate() will
indeed give you a string. I assume you simply want a date object with
today's date in it. Then:
var today = new Date();
var stYear = today.getFullYear();
Next syntax problem, "srEnroll is undefined". Indeed it is, it should
be stEnroll.
srEnroll[2] = john_nosbod; becomes stEnroll[2] = john_nosbod;
Next syntax problem, "stName is undefined", and also stSurname, etc.
(This is inside your FOR loop.) These items don't exist by themselves,
they are members of the student object. So you need to qualify them
with the student object. (And they themselves are not defined as arrays
inside student, so they don't take subscripts.)
Also, student.length is not important here. That's the length of ONE
student object. You've put the student objects into an array called
stEnroll. That's the thing you want the length of.
And, last but not least, your FOR is from 0 while <= length. That's one
too many. You need to go from 0 while < length.
So
for (i=0;i<=student.length;i+;+) {
lineString = ("<br /> " + stName[i] + " " + stSurname[i]);
lineString += (" " + stOUCU[i] + " YoB = " + stYoBirth[i] + "
");
lineString += (" Age = " + stAge[i] + " ");
document.write(lineString);
};
becomes
for (i=0;i<stEnroll.length;i+;+) {
lineString = ("<br /> " + stEnroll[i].stName + " " +
stEnroll[i].stSurname);
lineString += (" " + stEnroll[i].stOUCU + " YoB = " +
stEnroll[i].stYoBirth + " ");
lineString += (" Age = " + stEnroll[i].stAge + " ");
document.write(lineString);
};
I suppose your misunderstanding stems from "Array Objects". What you
have here are objects (student) which you happen to be putting into an
array (stEnroll). But the objects are not "array" themselves.
In fact, you could have left the individual students in their individual
variables and used them from there. E.g.
document.write("<br /> " + bob_sharp.stName + " " +
bob_sharp.stSurname +
" " + bob_sharp.stOUCU + " YoB = " +
bob_sharp.stYoBirth + " " +
" Age = " + bob_sharp.stAge + " ");
(outside your FOR loop) gives you Bob's information.
Dave
PS my complete version of yours (plus my extra copy of Bob's info) ...
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>(Type a title for your page here)</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<script language="JavaScript" type="text/javascript">
/* <![CDATA[ */
var today = new Date();
var stYear = today.getFullYear();
function student (stName,stSurname,stOUCU,stPI,stRegion,stYoBirth) {
this.stName = stName;
this.stSurname = stSurname;
this.stOUCU = stOUCU;
this.stPI = stPI;
this.stRegion = stRegion;
this.stYoBirth = stYoBirth;
this.stage = age;
this.stAge = age();
};
var john_wilson = new student ("John", "Wilson", "jw9372", "AB12345678",
1, 1986);
var bob_sharp = new student ("Bob", "Sharp","bs3578", "y9031527", 1,
1951);
var john_nosbod = new student ("John", "Nosbod", "nb1234", "z1234567",
1, 1952);
var peter_cams = new student ("Peter", "Camilleri", "pc9876",
"a9876543", 1, 1989);
var stEnroll = new Array;
stEnroll[0] = john_wilson;
stEnroll[1] = bob_sharp;
stEnroll[2] = john_nosbod;
stEnroll[3] = peter_cams;
function age() {
return (stYear - this.stYoBirth);
};
/* ]]> */
</script>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080&q