School Catalogue (front-end applications intensive, day 17): number of students lost along the way


#1

When I try to create the lorraineHansbury instance of the PrimarySchool class, somehow the instance remains blissfully unaware of the numberOfStudents I pass it as an argument. PrimarySchool inherits from the School class, which is created as follows:

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name;
    this._level = level;
    this._numberOfStudents = numberOfStudents;
  }
  
  get name() {
    return this._name;
  }
  
  get level() {
    return this._level;
  }
  
  get numberOfStudents() {
    return this._numberOfStudents;
  }
  
  set numberOfStudents(newStudentFigure) {
    if (typeof newStudentFigure === 'number') {
      this._numberOfStudents = newStudentFigure;
    } else {
      console.log('Invalid input: numberOfStudents must be set to a Number.');
    };
  }
  
  quickFacts() {
    console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.`);
  }
  
  static pickSubstituteTeacher(substituteTeachers) {
    let teacherNumber = Math.floor(Math.random() * substituteTeachers.length);
    //return substituteTeachers[teacherNumber];
    console.log(substituteTeachers[teacherNumber]);
  }
}

And here is how I created PrimarySchool:

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, numberOfStudents);
    this._level = 'primary';
    this._pickupPolicy = pickupPolicy;
  }
  
  get pickupPolicy() {
    return this._pickupPolicy;
  }
}

Here is me creating the lorraineHansbury instance of PrimarySchool:

const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

The 514 somehow does not get passed as an argument, so the quickFacts method of lorraineHansbury prints out that the school educates “undefined students at the primary level.” (The object does get the other two arguments). I’ve looked through my code and still have no idea what’s causing this. There is no error message; everything runs, but returns ‘undefined’ instead of 514. In other words, the numberOfStudents argument somehow does not get passed when I declare lorraineHansbury. Any idea why?


#2

in the constructor of PrimarySchool you do:

super(name, numberOfStudents);

which will call constructor of School, which if we take a look:

constructor(name, level, numberOfStudents)

has 3 parameters. Given you only provide two arguments for super, the third parameter of School constructor would be undefined.


#3

I tried passing the level to the parent class in PrimarySchool’s constructor, as follows:

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'primary', numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }
...

and everything worked.
Thanks!


#4