0

I have a Student, who is in many courses, which have many Modules for each course.

So far, I have got:

var myStudent = new MySchool.Student("Bart", "Simpson", "0800 Reverse", "Some Street", "Springfield");

myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding"));
myStudent.addCourse(new MySchool.Course("Q1111", "Driving"));
myStudent.addCourse(new MySchool.Course("D999", "Avoiding Detention"));

Student.JS

MyStudent.Student = function (firstName, lastName, tel, address1, address2) {
    this._firstName = firstName;
    this._lastName = lastName;
    this._tel = tel;
    this._address1 = address1;
    this._address2 = address2;
    this._courses = new Array();

};

//Add course:
addCourse: function (course) {
    this._courses.push(course);
},

This works fine. However, I would like to add Modules to this. So for each course there are multiple modules.

I have tried performing a multi-dimensional array but with no success.

Can someone please advise? Is there an alternative?

3
  • 1
    Where's your MySchool.Course? Why can't you just add another array to that for modules? Commented Nov 21, 2012 at 16:57
  • 6
    "Some Street" should be "742 Evergreen Terrace" Commented Nov 21, 2012 at 16:57
  • var sktCourse = new MySchool.Course(...); sktCourse.addModule(new MySchool.Module(...)); myStudent.addCourse(sktCourse); Commented Nov 21, 2012 at 17:02

3 Answers 3

1

Not exactly sure what you mean exactly but from what I understood, here is how you could do :

MyStudent.Student = function (firstName, lastName, tel, address1, address2) {
    this._firstName = firstName;
    this._lastName = lastName;
    this._tel = tel;
    this._address1 = address1;
    this._address2 = address2;
    this._courses = [];

    this.addCourse =  function (course) {
        this._courses.push(new Course(course));
    };
};


//Add course:

MySchool.Module = function(name){
    this.name = name;
}

MySchool.Course = function(name) {

    this.name = name;
    this.modules = [];

    this.addModule = function(name) {
        this.mmodules.push(new MySchool.Module(name));
    }
}

That way, you can create a student which has a function addCourse. Then you add whatever courses you want, and for each course you have a addModule function to fill them with modules.

You could do something more complex, like for exemple creating a student which takes as parameters an array of courses/modules, which would look like that :

courses = [
    "poney" : [
        "ride",
        "jump"
    ],
    "english" : [
        "oral",
        "grammar",
        "written"
    ]
]

And then create a function which loop through the array and uses the addCourse and addModule functions to fill your student with his courses/modules. But as you begin with JS, maybe you prefer the simple solution upside.

Sign up to request clarification or add additional context in comments.

Comments

0

The OO way of doing this would be to have a modules array in your course since the modules belong to the course. Then you would add modules to the course directly

Comments

0

Here's the quick and dirty:

addCourse: function (course) {
    course.modules = [];
    course.addModule = function(module){
        this.modules.push(module);
    }
    this._courses.push(course);
    return course;
}

which can be used like this:

myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding")).addModule(...);

Of course the best approach is to deal with all of this inside the Course constructor, which you haven't shown us.

1 Comment

///constructor.Course = function (courseCode, name) { this._courseCode = courseCode; this._name = name; this._modules = new Array(); };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.