School Catalogue - .quickFacts Function


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

I have no idea how to call this correctly.

School.quickfacts() returns:

School educates undefined students at the undefined school level.

I’ve tried adding a few different entries but unable to get it to fire with the correct response.

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

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'primary', numberOfStudents)
    this._pickupPolcy = pickupPolicy
  }
  
  get pickupPolcy() {
    return this._pickPolicy
  }
}

class MiddleSchool extends School {
  constructor(name, numberOfStudents) {
    super(name, 'middle', numberOfStudents)
  }
}

class HighSchool extends School {
  constructor(name, numberOfStudents, sportsTeam) {
    super(name, 'high', numberOfStudents)
    this._sportsTeam = sportsTeam
  }
  
  get sportsTeam() {
    return 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.')

console.log(School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']))

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

console.log(alSmith.sportsTeam)

School.quickFacts()
PrimarySchool.quickFacts()
School.quickFacts(lorraineHansbury )

#2

why is the quickFacts method static?


#3

I can’t get it to work otherwise.

It simply states:

/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:71
School.quickFacts(PrimarySchool)
       ^

TypeError: School.quickFacts is not a function
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:71:8)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

#4

but if we look at the instructions:

Call .quickFacts() on the lorraineHansbury instance.

you call the method on the class itself, not the instance. But that didn’t answer my question, why would you want a static method? The question was actually trying to imply that quickFacts shouldn’t be a static method. This does mean you will need to change the method calls


#5

Ah I see what you mean. I need to call it on the object once its created. Thank you!


#6

you are not entirely out of the woods yet, but its certainly a step in the right direction


#7

I added this._numberOfStudents = numberOfStudents to the constructor of each subclass to fix up the undefined issue.

My .quickfacts is reporting the correct response.