My two constructors are not working any help please

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

My two constructors are not working any help please

 
0
  #1
Jun 7th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: My two constructors are not working any help please

 
1
  #2
Jun 8th, 2009
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+
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Re: My two constructors are not working any help please

 
0
  #3
Jun 8th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 425 | Replies: 2
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC