Kush
Kush

Reputation: 196

JavaScript Multi-Dimensional Array

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?

Upvotes: 0

Views: 1708

Answers (3)

user1726343
user1726343

Reputation:

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.

Upvotes: 0

nialna2
nialna2

Reputation: 2224

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.

Upvotes: 1

zbrunson
zbrunson

Reputation: 1757

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

Upvotes: 0

Related Questions