School Catalogue // invoking setter on an instance

Hi again, friends. I’m having trouble with the setter on line 20 of this code for the School Catalogue project. In this case, I want to invoke the numberOfStudents setter on the lorraineHansbury instance to change the number of students after I’ve created it (using Parent class, constructor, and all). Nonetheless, I get TypeError: saying its not a function. Any help would be appreciated :slight_smile:

Code Here

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(num) {
    if (Number.isNaN(num)) {
    console.log('Invalid input: numberOfStudents must be set to a Number.');
    } else {
    return this._numberOfStudents = num;
    }
  } 

  quickFacts() {
    console.log(`${this.name} educates ${this.numberOfStudents} students at the ${this.level} level.`)
  }

  static pickSubstituteTeacher(substituteTeachers) {
    let tlength = substituteTeachers.length;
    console.log(substituteTeachers[Math.floor(tlength * Math.random())]);
    return substituteTeachers[Math.floor(tlength * Math.random())];
  }
}

class PrimarySchool extends School {
  constructor(name, num, pickupPolicy) { 
    super(name, 'primary'); 
    this.numberOfStudents = num; // invoke the setter method
    this._pickupPolicy = pickupPolicy;
  }

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

  get pickupPolicy() {
    return this._pickupPolicy;
  }

}

class Middle extends School {
  constructor(name, numberOfStudents) {
    super(name, 'Middle', numberOfStudents);
  }
}

class High extends School {
  constructor(name, numberOfStudents, sportsTeam) {
    super(name, 'high', numberOfStudents);
    this._sportsTeam = sportsTeam;
  }

  get sportsTeam() {
    return console.log(this._sportsTeam);
  }

}

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');
lorraineHansbury.quickFacts();
School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);

//lorraineHansbury.numberOfStudents(12);
console.log(lorraineHansbury.numberOfStudents);

const alSmith = new High('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);
alSmith.sportsTeam;

const johnson = new Middle('Johnson Junior High School', 312);


1 Like

Solved my own problem:

lorraineHansbury.numberOfStudents(5) // is not how you would use a setter

lorraineHansbury.numberOfStudents = 5 // how you use a setter

3 Likes

Had the same problem, glad you solved it. I’m surprised the project instructions don’t ask students to test this setter.