Hello, my task was to make two constructors for the code that I also posted and they are not working. Can somebody help me figure out why they are not working. I posted them right below.

here are my constructors for course and Faculty

course.js:

function Course (name, times, instructorName, instructorDept ) {
this.name = name ||"unknown course name";
this.times = times||"unknown time";
this.instructor = new Faculty(instructorName, instructorDept);


this.getname = function();
this.setname = function( name );

this.gettimes = function();
this.settimes = function( dept );

this.getinstructor() = function();
this.setinstructor(Faculty);

this.name.toString = function();
}

faculty.js:
function Faculty(name, dept) {
this.name = name || "unknown";
this.dept = dept || "unknown";

this.getname = function();
this.setname = function( name );

this.getdata = function();
this.setdata = function( dept );

this.name.toString = function();
this.dept.toString = function();
}

Here is the test file testprob1.js:

var fac1 = new Faculty( "Dr. Smith", "Math" );

document.writeln( "New faculty: " + fac1.getname() + ", " + fac1.getdept() + "<br>" );

document.writeln( "Again: " + fac1 + "<br><br>" );

var fac2 = new Faculty();

document.writeln( "New faculty: " + fac2.getname() +

", " + fac2.getdept() + "<br>" );

fac2.setname( "Dr. Jones" );

fac2.setdept( "CS" );

document.writeln( "Updated: " + fac2.getname() +

" " + fac2.getdept() + "<br><br>" );

var crs1 = new Course( "COMP322", "MWF 1:00-1:50",

"Dr. Green", "CS" );

document.writeln( "New course: " + crs1.getname() +

", " + crs1.gettimes() +

", " + crs1.getinstructor() + "<br>" );

document.writeln( "Again: " + crs1 + "<br><br>" );

var crs2 = new Course();

document.writeln( "New course: " + crs2.getname() +

", " + crs2.gettimes() +

", " + crs2.getinstructor() + "<br><br>" );

crs2.setname( "COMP590" );

crs2.settimes( "TR 1:00-2:15" );

crs2.setinstructor( fac2 );

document.writeln( "Updated: " + crs2.getname() +

", " + crs2.gettimes() +

", " + crs2.getinstructor() );

The HTML drivers to run the code:

<html>

<head>

<title>Assignment 3, Problem 3</title>

<script type="text/javascript" src="faculty.js">

</script>

<script type="text/javascript" src="course.js">

</script>

<script type="text/javascript" src="testProb1.js">

</script>

</head>

</html>

Recommended Answers

All 2 Replies

I've shortened out your code and skipped some of the unecessary lines inside the two constructors.

You'll need to run this entire document, to see the actual effects of the two functions.

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>
<style type="text/css">
<!--
p { border-bottom : 2px solid #eee; color : #800080; line-height : 1.6; padding-bottom : .500em; margin-left: .700em; letter-spacing : 3px; margin-top : 2em; white-space : nowrap; }
span { color : #365d95; }
-->
</style>
<script type="text/javascript">
<!--
var Faculty = function( name, dept ) {
   this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" );

   this.dept = (( dept ) ? dept : "undefined " + ( typeof( this ) ) + ": dept" );
   
   /* Commented Block Started

   this.getname = function( name ) { }; // Creating Empty function. And also you forgot to include brackets, so that's why it's messing the whole program.

   this.setname = function( name ) { }; 
   
   this.getdata = function() { };
   this.setdata = function( dept ) { };
   this.name.toString = function() { };
   this.dept.toString = function() { };  
  */// Commented Block Ended
};


var Course = function( name, times, instructorName, instructorDept ) {
var instructor;
   this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" );

   this.times = (( times ) ? times : "undefined " + ( typeof( this ) ) + ": times" );

   this.instructor = new Faculty( instructorName, instructorDept );

/* this.getname = function() { };
this.setname = function( name ) { };

this.gettimes = function() { };
this.settimes = function( dept ) { };

this.getinstructor() = function();
this.setinstructor(Faculty) { };

this.name.toString = function() { }; */

};

// -->
</script>
</head>
<body>
<div id="main">
<script type="text/javascript">
<!-- 
/* Contructor Faculty() Usage Example: */

var fac1 = new Faculty("Dr. Smith", "Math" );

   document.writeln("\n<p>New Faculty: <span>" + (( fac1.name ) ? fac1.name : fac1["name"] ) + "</span><br>Department: <span>" + (( fac1.dept ) ? fac1.dept : fac1["dept"] ) + "</span><br>" );

   document.writeln("Again: <span>" + fac1.name + "</span>, <span>" + fac1.dept + "</span></p>\n" );

var fac2 = new Faculty();
   fac2["name"] = "Dr. Jones";
   fac2["dept"] = "CS";

   document.writeln("\n<p>New Faculty: <span>" + (( fac2.name ) ? fac2.name : fac2["name"] ) + "</span><br>Department: <span>" + (( fac2.dept ) ? fac2.dept : fac2["dept"] ) + "</span></p>\n" );

var crs1 = new Course("COMP332", "MWF 1: 00-1:50", "Dr. Green", "CS" );

   document.writeln("<p>New course: <span>" + (( crs1.name ) ? crs1.name : crs1["name"] ) + "</span><br>Time: <span>" + (( crs1.times ) ? crs1.times : crs1["times"] ) + "</span><br>Instructor: <span>" );

   document.writeln( crs1.instructor.name + "</span><br>" + "Department: <span>" + crs1.instructor.dept + "</span></p>" ); 

// -->
</script>
</div>
</div> 
</body>
</html>

Tested in OPERA8+

commented: Kool guy +2

Thanks a lot. Even though the bottom part doesn't show up, I can try to figure it out with the stuff you gave me all ready.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.