Project: School Catalogue. How to call the method in step 15?


#1

Hello everyone,
I’m stuck at step 15: Call .quickFacts() on the lorraineHansbury instance. I’m not sure if I have created the right quickFacts() method in the parent class School. When I call the method, I had to manually change the variables, which doesn;t seem correct. Any suggestions on improving my codes?

Here is my source code:

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;
  }
  
  //Setter: check if numberOfStudents is a number 
  set numberOfStudents(newNumberOfStudents) {
    if (typeof newNumberOfStudents === 'number') {
      this._umberOfStudents = newNumberOfStudents
    } else {console.log('Invalid input: numberOfStudents must be set to a Number.')}
  }
  
  quickFacts(name, numberOfStudents, level){
    console.log(`${name} educates ${numberOfStudents} students at the ${level} school level.`);
  }
  
  static pickSubstituteTeacher(substituteTeachers){
    let randomTeacher = Math.floor(substituteTeachers.length * Math.random() - 1);
    return substitueTeachers[randomTeacher];
  }
  
}

//Subclass: PrimarySchool

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

//Subclass: HighSchool

class HighSchool  extends School {
  constructor(name, numberOfStudents, sportsTeams){
    super(name);
    this._numberOfStudents =  numberOfStudents;
    this._sportsTeams = sportsTeams;
    this._level = 'high';}
  
  get sportsTeams() {
    return this._sportsTeams = sportsTeams;
  } 
}

//Create Instances

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('Lorraine Hansbury', 514, 'high')

#2

There is one error spotted. May be more. Still scanning.

Since this is a method, it will have no need of parameters. The variables all belong to the instance, already.

class SomeClass {
  constructor (name, something, anything) {
    this._name = name;
    this._something = something;
    this._anything = anything;
  }
  get name() {
    return this._name;
  }
  get something() {
    return this._something;
  }
  get anything() {
    return this._anything;
  }
  set name(newName) {
    this._name = newName;
  }
  set something(newSomething) {
    this._something = newSomething;
  }
  set anything(newAnything) {
    this._anything = newAnything;
  }
  quickFacts(){
    console.log(`${this.name} educates ${this.something} students at the ${this.anything} school level.`);
  }
}
var instance = new SomeClass('Rube','Icks','Cube');
instance.quickFacts();
Rube educates Icks students at the Cube school level.

Remember to query this for the instance variables, and as long as there are getters, then use the getter name, not the instance variable name.


#3

Hello mtf,

Thank you very much for taking your time answering my question and pointing out my errors. I corrected my code and it worked!

However my if/else statement in the setter doesn’t seem to work correctly. I’m unable to spot out what went wrong… It seems whatever input I give it, it goes True.

//Setter: check if numberOfStudents is a number 

 set numberOfStudents(newNumberOfStudents) {  
    if (typeof newNumberOfStudents === 'number') {
      this._numberOfStudents = newNumberOfStudents
    } else {console.log('Invalid input: numberOfStudents must be set to a Number.')}
  } 
  
var lorraineHansbury = new PrimarySchool ('Lorraine Hansbury','SO MANY', 'anything')
lorraineHansbury.quickFacts()

Lorraine Hansbury educates SO MANY students at the primary school level.


#4

This argument should be a number.