School Catalogue - Excercise #5 - Setter method returning "undefined" when creating an instance of the School Class

Link to the exercise

I hope I’m making this post as clear as possible per the codecademy rules. I created my parent class: School, and all of the subclasses: PrimarySchool and HighSchool. When I console.log() to look at the values stored in the instances, they all show up except for the numberOfStudents value.

I think it has to do with the setter. I’ve played around with different configurations, and added things, and I even used the isNaN() method instead of typeof. Embaressed to say this is my 2nd day working on this. Oh, and I also researched within the fourms here for an answer and nothing with my specific issue.

    class School {
      constructor(name, numberOfStudents, level){
        this._name = name;
        this._numberOfStudents = numberOfStudents;
        this._level = 'primary', 'middle', 'high';
    
      } 

        get name(){
        return this._name;
      }

        get level(){
        return this._level;
  }

        get numberOfStudents(){
        return this._numberOfStudents;
      }
  
      quickFacts(){
        console.log(`${this.name} educates ${this.numberOfStudents} students at the ${this.level} school level.`)
      }

      static pickSubstituteTeacher(substituteTeachers){
        let subIndex = Math.floor(Math.random() * substituteTeachers.length -1);
        return substituteTeachers[subIndex];
      }

     set numberOfStudents(value){   // <---This setter method is returning a value of undefined.
        if(typeof value === number){
         this._numberOfStudents = value ;
        }else{
          console.log('Invalid input: numberOfStudents must be set to a Number.');
        }
      }
    } 
  
     class PrimarySchool extends School{
      constructor(name, numberOfStudents, pickupPolicy){
        super(name);
        this._pickupPolicy = pickupPolicy;
      }

      get pickupPolicy(){
        return this._pickupPolicy;
      }
    }

    class HighSchool extends School{
      constructor(name, numberOfStudents, sportsTeams){
        super(name);
        this._sportsTeams = sportsTeams
      }

      get sportsTeams(){
        console.log(this.sportsTeams);
      }
    }

    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.')

    console.log(lorraineHansbury);
/*  When I call this console.log() I get this in the console.

PrimarySchool {
  _name: 'Lorraine Hansbury',
  _numberOfStudents: undefined,  // <--Why undefined here?
  _level: 'primary',
  _pickupPolicy: 'Students must be picked up by a parent, guardian, or a family member over the age of 13.' }
*/

I think the constructors might be a problem:

    class School {
      constructor(name, numberOfStudents, level){
        this._name = name;
        this._numberOfStudents = numberOfStudents;
        this._level = 'primary', 'middle', 'high';
      } 

I think the last line of that constructor should be

this._level = level;

otherwise the constructor may not set the appropriate string for ._level

And for the PrimarySchool class,

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

You’re not using the super function with all the required arguments …

super(name, numberOfStudents, 'primary'); 

would be needed in order for all of those to be defined;
because the constructor for School has 3 parameters.

1 Like

Thank you so much! I really appreciate the help. You’re right, it was both of those issues. The constructor AND the super. I fixed both and the catelogue works like a charm.

Here’s a snippit of the code I fixed so others can see the fix.
The constructor method

class School {
  constructor(name, numberOfStudents, level){
    this._name = name;
    this._numberOfStudents = numberOfStudents;
    this._level = level;  // fixed this part
    
  } 

The supers are fixed now too.

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

The other super

class HighSchool extends School{
  constructor(name, numberOfStudents, sportsTeams){
    super(name, numberOfStudents, 'high'); //fixed this too.
    this._sportsTeams = sportsTeams
  }
1 Like