School Catalogue

Hi! I have a problem with this exercise!
Link: https://www.codecademy.com/courses/introduction-to-javascript/projects/school-catalog
When i call quickFacts() the result is :undefined educates undefined students at the undefined school level.
But i don’t understand why!
Can we help me?
Thank’s.
This is the code:

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

///Primary
class Primary extends School{
  constructor(name, level, numberOfStudents, pickupPolicy){
    super(name, level, numberOfStudents)
    this._pickupPolicy = pickupPolicy
  }
  get pickupPolicy(){
    return this._pickupPolicy
  }
};
///Middle
class Middle extends School{
  constructor(name, level, numberOfStudents){
    super(name, level, numberOfStudents)
  }
};
///High
class High extends School{
  constructor(name, level, numberOfStudents, sportsTeams){
    super(name, level, numberOfStudents)
    this._sportsTeams = sportsTeams
  }
  get sportsTeams(){
    return this. _sportsTeams
  }
};

const lorraineHansbury = new Primary('Lorraine Hansbury', 'primary', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');
console.log(lorraineHansbury._name)
Primary.quickFacts()
School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);

const alSmith = new High('Al E. Smith', 'high', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);
console.log(alSmith._sportsTeams)
1 Like

you should call the quickFacts method on the instance of your primary school (lorraineHansbury), not on the class itself.

the class instance (lorraineHansbury) set the instance variables through the constructor.

1 Like

Thx for the reply.
When i call with: “lorraineHansbury.quickFacts()” the problem is: “TypeError: lorraineHansbury.quickFacts is not a function”
I found the solution:
I remove from “static quickFacts()” the static method and it work, but why?
EDIT
Ok i found.
The exercise say: " Methods : .quickFacts() and .pickSubstituteTeacher() (this is a static method)"
But only the last one is a static method.

1 Like
//https://www.youtube.com/watch?v=9p2VeKRKZ1Q&feature=emb_title
//main 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;
 }
 //setter
 set numberOfStudents(value) {
   if (value.isNaN()) {
     console.log("Invalid input: numberOfStudents must be set to a Number.");
   } else {
     this._numberOfStudents = value;
   }
 }
 //method
 quickFacts() {
   console.log(`${this.name} school, educates ${this.numberOfStudents} students at the ${this.level} school level.`);
 }
 //static method
 static pickSubstituteTeacher(substituteTeachers) {
   const randInt = Math.floor(Math.random() * substituteTeachers.length);
   return substituteTeachers[randInt];
 }
}
//subclass PrimarySchool
class PrimarySchool extends School {
 constructor(name, numberOfStudends, pickupPolicy) {
   super(name, 'primary', numberOfStudends);
   this._pickuPolicy = pickupPolicy;
 }
 get pickupPolicy() {
   return this._pickupPolicy;
 }
}
//subclass HighSchool
class HighSchool extends School {
 constructor(name, numberOfStudends, sportsTeams) {
   super(name, 'high', numberOfStudends);
   this._sportsTeam = sportsTeams;
 }
 get sportsTeam() {
   return this._sportsTeam;
 }
}
 
// new instance
const lorraineHansbury = new PrimarySchool("Lorraine Hansbury", 512, "Students must be picked up by a parent, guardian, or a family member over the age of 13.");
lorraineHansbury.quickFacts();
 /*
const sub = School.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli"
]);
console.log(sub);
 */ 

// new instance
const alSmith = new HighSchool('Al E. Smith', 415, ["Baseball","Basketball","Volleyball","Track and Field"]);
alSmith.quickFacts();
console.log(alSmith.sportsTeam);
 
const noNameSchool = new PrimarySchool("No name school", 'AAA', "blablalaa");
noNameSchool.quickFacts();

fixed that issue.
now line 77: returns No name school school, educates AAA students at the primary school level. but should return error, I’ve uses ‘AAA’ instead of integer value. thanks

in the constructor you use this._sportsTeam while in the getter you use this._sportsTeams, so two different properties (single vs plural)

the setter would work, but its never used. Where do you use the setter?

1 Like

fixed that issue with this._sportsTeam :ok_hand:
now the setter isn’t working line 77:
I’ve created new obj (noNameSchool ) - which is instance of PrimarySchool which extends from main School class
So I’ve pass sting ‘AAA’ as numberOfStudends, and it should return error, because in School class
we have this setter:

set numberOfStudents(value) {
   if (value.isNaN()) {
     console.log("Invalid input: numberOfStudents must be set to a Number.");
   } else {
     this._numberOfStudents = value;
   }

it logs an error, doesn’t return an error

but the setter is never used anywhere? The constructor uses the underlying property _numberOfStudents directly, it doesn’t use the getter and setter. And JS allows this (shame)

I don’t get it, so what’s the point of

set numberOfStudents(value) {
   if (value.isNaN()) {
     console.log("Invalid input: numberOfStudents must be set to a Number.");
   } else {
     this._numberOfStudents = value;
   }

this validation, if it still runs, with the string as numberOfStudents ?

You could throw an exception instead, that is up to the programmer to decide how to handle cases that are not allowed

but the point still stands, you never use the setter. You declared/defined the setter, but that is it

so how can I use it/call it in this example?
I guess, the lesson was designed, to output the error if number of student is string, but it didn’t

well, here:

this._numberOfStudents = numberOfStudents;

you set the property directly, instead of using the setter. How could one use a setter? Can you give me an example?

what is gone? The problem? :slight_smile: