An array contains similar elements in a large quantity. In JavaScript, they
are numbered from 0 to n-1 where n is the length of the array. If the array
has the name allemployees
, then one can access the first
element as allemployees[0]
, the second element as
allemployees[1]
, and so on. The length of the array can be
obtained by a method belonging to the array object as follows:
n = allemployees.length
- the methods referring to the object
allemployees
are all marked by adding a dot .
and
the name of the method to the variable name allemployees
. In
this case the method has the name length
.
A record contains various datas belonging to the same object. Accessing
these data is in JavaScript the same as accessing a method - the only
difference is that one can also write values into the method which is
normally not possible. The word object
refers to both: the abstract
data type it represents and a variable taking an instance of this data
type. In this assignment, the employee
data type is defined by
the following constructor function employee(fn, sl, io)
as:
function employee(fn, sl, io)
{
this.fullname = fn;
this.salary = sl;
this.isowner = io;
}
The this
keyword refers to the object created by the
constructor function and this.fullname
to the value the full
name field will have. Now, one can create an employee emp
as
follows:
var emp = employee("Gerald Gugelhupf", 2233.44, false);
who then has the full name Gerald Gugelhupf
, earns SGD 2233.44
per month but is not considered to be an owner.
The data structure in this assignment is an array of records. It is initialized as follows:
allemployees = new Array(5);
allemployees[0] = new employee("Anneliese Aal", 1324.35, false);
allemployees[1] = new employee("Boris Bratling", 1122.44, false);
…
So allemployees
consists of 6 entries with indices 0 to 5.
The data stored in the first two entries is the following:
allemployees[0]
—
Employee Name: Anneliese Aal; Salary: SGD 1324.35; Not an owner.
allemployees[1]
—
Employee Name: Boris Bratling; Salary: SGD 1122.44; Not an owner.
The following boolean condition is true:
allemployees[1].fullname == "Boris Bratling"
So the dot symbol and the field name for the record come after the index number to identify the correct array element.
Please refer to this page for information on how to work on your assignment.
Each employee should have an age. So change the employee()
function such that it also assigns a number to this.age
. This
number should be a parameter of the calling functions. Add for each
employee a value for the age; it should be some number between 25 and 65.
Adapt the employeeprint()
function such that it also prints
the age of the employee.
Modify the length of the array to 7. Furthermore, assign to the new
employee the name Herbert Hering
and the salary SGD 1100.22.
His age is 35.
Change the program such that Eberhard Ei and Claudia Creme are both owners of the company. Both owners have a salary from their own company (as already implemented).
It should scan through the allemployees
array and list those
employees which are owners. You can read the code for the function to sum
the salary for seeing how this is done.
Slides 48 - 58 of the Third Lecture: ps-file and pdf-file.
Pages 33-35 of Chapter 2 in Algorithmics, The Spirit of Computing.
Modules 8 and 11 in Java Script, A Beginner's Guide.