School Catalogue: My values are undefined

Hi, I’m having trouble figuring out why my values are undefined.

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/school-catalog

My code:

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

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

class High extends School {
  constructor(name, numberOfStudents, sportsTeams) {
    super(name);
    this._level = 'high';
    this._numberOfStudents = numberOfStudents;
    this._sportsTeams = [];
  }
  
  get sportsTeams() { console.log(this._sportsTeams); }
}

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

The output is as follows: undefined educates undefined students at the undefined school level.

all the properties that are undefined, are within the school constructor, so lets see when we attempt to log something:

contructor(name, level, numberOfStudents) {
    console.log('get here');
    this._name = name;
    this._level = level;
    this._numberOfStudents = numberOfStudents;
  }

which outputs nothing, we do expect output (the constructor should be called), so how could that be?

1 Like

Aren’t those properties supposed to be inherited from the parent constructor? Rubenz used the function super() to do that yet they are not inherited.

yes, that is suppose to be the case. But as evident by the log statement, the constructor is never reached. Which indicates a typo in the the word constructor. (this name has to be correctly spelled in order to work)

I had just spend so long trying to figure out why I kept getting undefined for my numberOfStudents. It was a typo which this thread helped me realise. So thanks for your indirect help :smiley:

1 Like