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

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

My result is working as it should, unless the number of students.

** here is the result of my code:**

Lorraine Hansbury educates undefined students at the primary school level
[ ‘Baseball’, ‘Basketball’, ‘Volleyball’, ‘Track and Field’ ]

here is the full code:

//creates main Class School

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(value) {
  if (value.isNan()) {
   console.log('Invalid input: numberOfStudents must be set to a Number.') 
  } 
  else {
    this._NumberOfStudents = value
   }
  }

  quickFacts() {
    console.log(`${this._name} educates ${this._number} students at the ${this._level} school level`);
  }

  static  pickSubstituteTeacher(substituteTeachers) {
   const randInt = Math.floor(substituteTeachers.length * Math.random());
   return substituteTeachers[randInt]; 
   }
}

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

get pickupPolicy(){
  return this._pickupPolicy
}
}

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


  get sportsTeams(){
    return this._sportsTeams
//console.log(this._sportsTeams)
  }

}
//end of the High School Class
//----------------------//

//Create a PrimarySchool instance 
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.');
//WHY STUDENTS NUMBER IS UNDEFINED?
lorraineHansbury.quickFacts()

//SET LIST OF SUBSTITUTE TEACHERS
School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli'])

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

console.log(alSmith.sportsTeams)

The issue is in your quickFacts() definition.

You are looking for this._number, instead of this.numberOfStudents. Fix this and it fixes the issue.

Side question: You have defined getters for each of the properties, so why have you decided to draw from the property directly using eg. this._name? It doesn’t affect your code at all in this instance, however it is good practise to get in the way of using the getters and setters instead of drawing directly from the property.

Thank you, it solved the issue.

About your second point, I never actually know when it is mandatory to use or not use .this

Technically it’s never really mandatory, but what I would say is you start by declaring your object, in which you declare the properties, and then the getters and setters. Then anything below where the getters and setters are declared, you can start using those, including inside of the object. Therefore in your declaration for the quickFacts() function you can use your getters by doing this.name instead of this._name.

Would anyone be willing to help me spot the mistake here? It’s to do with the numberOfStudents property, and I got this error when creating a new Primary School instance:

/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:52
    super(numberOfStudents);
    ^

ReferenceError: this is not defined
    at new PrimarySchool (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:52:5)

Here is my full code:

//School parent class 

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;
  }

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

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

  set numberOfStudents(newNumberOfStudents){
    if (newNumberOfStudents !== NaN) {
      this._numberOfStudents = newNumberOfStudents
    } else { console.log('Invalid input: numberOfStudents must be set to a number.')}
  }

  quickFacts(){

  }

  pickSubstituteTeacher(){

  }
}

//Primary School class

class PrimarySchool extends School{

  constructor(name, numberOfStudents, pickUpPolicy){
    super(name);
    super(numberOfStudents);
    this._level = 'primary';
    this._pickUpPolicy = pickUpPolicy;
  }

  get pickUpPolicy(){
    return this._pickUpPolicy;
  }
}

//Middle School class

class MiddleSchool extends School{

}

//High School class

class HighSchool extends School{

  constructor(name, numberOfStudents, sportsTeams){
    super(name);
    super(numberOfStudents);
    this.level = 'high';
    this._sportsTeams = sportsTeams;
  }

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

//Lorraine Hansbury school

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

When using super you want to incorporate all of the inherited properties into a single super statement. Therefore instead of what you have in the constructor at the moment, you would want:

constructor(name, numberOfStudents, pickUpPolicy){
    super(name, 'primary', numberOfStudents);
    this._pickUpPolicy = pickUpPolicy;
  }

This inherits name, level and numberOfStudents, and sets level to be 'primary'. You can then use this same thing for your other two classes, just changing the level as appropriate.

Yes! I had forgotten this. Thank you so much :grin:

1 Like