943,626 Members | Top Members by Rank

Ad:
Jun 7th, 2009
0

My two constructors are not working any help please

Expand Post »
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:
javascript Syntax (Toggle Plain Text)
  1. function Course (name, times, instructorName, instructorDept ) {
  2. this.name = name ||"unknown course name";
  3. this.times = times||"unknown time";
  4. this.instructor = new Faculty(instructorName, instructorDept);
  5.  
  6.  
  7. this.getname = function();
  8. this.setname = function( name );
  9.  
  10. this.gettimes = function();
  11. this.settimes = function( dept );
  12.  
  13. this.getinstructor() = function();
  14. this.setinstructor(Faculty);
  15.  
  16. this.name.toString = function();
  17. }
  18.  
  19. faculty.js:
  20. function Faculty(name, dept) {
  21. this.name = name || "unknown";
  22. this.dept = dept || "unknown";
  23.  
  24. this.getname = function();
  25. this.setname = function( name );
  26.  
  27. this.getdata = function();
  28. this.setdata = function( dept );
  29.  
  30. this.name.toString = function();
  31. this.dept.toString = function();
  32. }
  33.  
  34. Here is the test file testprob1.js:
  35.  
  36. var fac1 = new Faculty( "Dr. Smith", "Math" );
  37.  
  38. document.writeln( "New faculty: " + fac1.getname() + ", " + fac1.getdept() + "<br>" );
  39.  
  40. document.writeln( "Again: " + fac1 + "<br><br>" );
  41.  
  42. var fac2 = new Faculty();
  43.  
  44. document.writeln( "New faculty: " + fac2.getname() +
  45.  
  46. ", " + fac2.getdept() + "<br>" );
  47.  
  48. fac2.setname( "Dr. Jones" );
  49.  
  50. fac2.setdept( "CS" );
  51.  
  52. document.writeln( "Updated: " + fac2.getname() +
  53.  
  54. " " + fac2.getdept() + "<br><br>" );
  55.  
  56. var crs1 = new Course( "COMP322", "MWF 1:00-1:50",
  57.  
  58. "Dr. Green", "CS" );
  59.  
  60. document.writeln( "New course: " + crs1.getname() +
  61.  
  62. ", " + crs1.gettimes() +
  63.  
  64. ", " + crs1.getinstructor() + "<br>" );
  65.  
  66. document.writeln( "Again: " + crs1 + "<br><br>" );
  67.  
  68. var crs2 = new Course();
  69.  
  70. document.writeln( "New course: " + crs2.getname() +
  71.  
  72. ", " + crs2.gettimes() +
  73.  
  74. ", " + crs2.getinstructor() + "<br><br>" );
  75.  
  76. crs2.setname( "COMP590" );
  77.  
  78. crs2.settimes( "TR 1:00-2:15" );
  79.  
  80. crs2.setinstructor( fac2 );
  81.  
  82. document.writeln( "Updated: " + crs2.getname() +
  83.  
  84. ", " + crs2.gettimes() +
  85.  
  86. ", " + crs2.getinstructor() );

The HTML drivers to run the code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2.  
  3. <head>
  4.  
  5. <title>Assignment 3, Problem 3</title>
  6.  
  7. <script type="text/javascript" src="faculty.js">
  8.  
  9. </script>
  10.  
  11. <script type="text/javascript" src="course.js">
  12.  
  13. </script>
  14.  
  15. <script type="text/javascript" src="testProb1.js">
  16.  
  17. </script>
  18.  
  19. </head>
  20.  
  21. </html>
Last edited by Tekmaven; Jun 7th, 2009 at 10:05 pm. Reason: Code Tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Jun 8th, 2009
1

Re: My two constructors are not working any help please

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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html id="html40L" lang="en">
  4. <head>
  5. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta http-equiv="Content-Script-Type" content="text/javascript">
  8. <title>Live Help!</title>
  9. <style type="text/css">
  10. <!--
  11. 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; }
  12. span { color : #365d95; }
  13. -->
  14. </style>
  15. <script type="text/javascript">
  16. <!--
  17. var Faculty = function( name, dept ) {
  18. this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" );
  19.  
  20. this.dept = (( dept ) ? dept : "undefined " + ( typeof( this ) ) + ": dept" );
  21.  
  22. /* Commented Block Started
  23.  
  24.   this.getname = function( name ) { }; // Creating Empty function. And also you forgot to include brackets, so that's why it's messing the whole program.
  25.  
  26.   this.setname = function( name ) { };
  27.  
  28.   this.getdata = function() { };
  29.   this.setdata = function( dept ) { };
  30.   this.name.toString = function() { };
  31.   this.dept.toString = function() { };
  32.   */// Commented Block Ended
  33. };
  34.  
  35.  
  36. var Course = function( name, times, instructorName, instructorDept ) {
  37. var instructor;
  38. this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" );
  39.  
  40. this.times = (( times ) ? times : "undefined " + ( typeof( this ) ) + ": times" );
  41.  
  42. this.instructor = new Faculty( instructorName, instructorDept );
  43.  
  44. /* this.getname = function() { };
  45. this.setname = function( name ) { };
  46.  
  47. this.gettimes = function() { };
  48. this.settimes = function( dept ) { };
  49.  
  50. this.getinstructor() = function();
  51. this.setinstructor(Faculty) { };
  52.  
  53. this.name.toString = function() { }; */
  54.  
  55. };
  56.  
  57. // -->
  58. </script>
  59. </head>
  60. <body>
  61. <div id="main">
  62. <script type="text/javascript">
  63. <!--
  64. /* Contructor Faculty() Usage Example: */
  65.  
  66. var fac1 = new Faculty("Dr. Smith", "Math" );
  67.  
  68. 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>" );
  69.  
  70. document.writeln("Again: <span>" + fac1.name + "</span>, <span>" + fac1.dept + "</span></p>\n" );
  71.  
  72. var fac2 = new Faculty();
  73. fac2["name"] = "Dr. Jones";
  74. fac2["dept"] = "CS";
  75.  
  76. 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" );
  77.  
  78. var crs1 = new Course("COMP332", "MWF 1: 00-1:50", "Dr. Green", "CS" );
  79.  
  80. 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>" );
  81.  
  82. document.writeln( crs1.instructor.name + "</span><br>" + "Department: <span>" + crs1.instructor.dept + "</span></p>" );
  83.  
  84. // -->
  85. </script>
  86. </div>
  87. </div>
  88. </body>
  89. </html>

Tested in OPERA8+
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jun 8th, 2009
0

Re: My two constructors are not working any help please

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: The use of isNAN
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Using AJAX results in PHP form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC