School Catalogue

Hi, I’m hoping someone can clarify my code. I’ve just finished everything from the School Catalogue project, and it works! Where I’m confused is if I comment out my last console.log statement, all of my results are undefined:

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(newNumberOfStudent){
    if(newNumberOfStudents === number){
      this._numberOfStudents = newNumberOfStudents;
    } else {
      console.log(`Invalid input: ${this._numberOfStudents} must be set to a Number.` )
    }
  }
  
  quickFacts(){
    console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.`)
  }
  
  static pickSubstituteTeacher(substituteTeachers){
    let substituteTeacherIndex = Math.floor(Math.random() * substituteTeachers.length -1)
    return substituteTeachers[substituteTeacherIndex]
  }
  
};

class PrimarySchool extends School{
  constructor(name, numberOfStudents, pickupPolicy){
  super (name);
  this._level = 'primary';
  this._numberOfStudents = 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(sportsTeams)
    
  }
 
};

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(lorraineHansbury.quickFacts());
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.sportsTeams)

Could someone please explain how my last console.log statement (alSmith.sportsTeams) “allows” my previous, “console.log(School.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, ‘James Harden’, ‘Jason Terry’, ‘Manu Ginobli’]));” to produce a result? I’m confused as to why, when I comment out console.log(alSmith.sportsTeams), all other logs are undefined with the exception of “console.log(lorraineHansbury.quickFacts());”

How does one log impact the other?

Thanks!

Please disregard. Total brainfart. However, I am still receiving an undefined result logged to my console:

“Lorraine Hansbury educates 514 students at the primary school level.
undefined
Jason Terry
[ ‘Baseball’, ‘Basketball’, ‘Volleyball’, ‘Track and Field’ ]”

I’m guessing this is because within lorraineHasbury PrimarySchool instance, there’s a pickupPolicy string (‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’) that does not exist within the quickFacts method?

methods and function always return undefined, unless you use the return keyword to return something else.

you don’t use return in quickFacts method, so the default value (undefined) is returned, by calling console.log on quickFacts function call, you log the returned result (undefined) to the console

1 Like

For the School Catalogue Project, ‘if a number’ doesn’t appear to be working for ‘quickfacts’ function. Also, for ‘pickSubstituteTeacher’ function, random number is not being generated. Please assist for code posted below:

class School {

  constructor(name, level, numOfStudents) {

    this._name = name; 

    this._level = level;

    this._numOfStudents = numOfStudents;

  }

  get name() {

    return this._name;

  }

  get level() {

    return this._level;

  }

  get numOfStudents() {

    return this._numOfStudents;

  }

  set numOfStudents(value) {

    if (typeof value === number){

      this._numOfStudents = value;

    } else {

       console.log('Invalid input: Enter a Number.');         

    }

  }

 

  quickFacts() {

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

  }

 

  static pickSubstituteTeacher(substituteTeachers) {

    substituteTeachers = [];

    const i = Math.floor(substituteTeachers.length * Math.random());

    console.log(substituteTeachers[i]);

  }

}

class PrimarySchool extends School {

  constructor(name, numOfStudents, pickupPolicy) {

    super(name, 'primary', numOfStudents);

    this._pickupPolicy = pickupPolicy;

  }

  get PrimarySchool() {

    return this._pickupPolicy;

  }

}

class HighSchool extends School {

  constructor(name, numOfStudents, sportsTeam) {

    super(name, 'high', numOfStudents);

    this._sportsTeam = sportsTeam;

  }

  get sportsTeam() {

    console.log(sportsTeam);

  }

}

const Prim1 = new PrimarySchool('Lorraine Hansbury', 'test', 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')

Prim1.quickFacts();

const sub = School.pickSubstituteTeacher['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli'];

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

console.log(High1._sportsTeam);

High1.quickFacts();

Thank you

you never use the setter in the constructor. The constructor bypasses the setter, which JS allows (no proper encapsulation enforcement)

not sure what you are doing here. You have a static method with a parameter:

static pickSubstituteTeacher(substituteTeachers) 

then you overwrite the parameter with an empty list:

substituteTeachers = [];

so whatever argument you provided for the method has now become useless.

then the call itself:

const sub = School.pickSubstituteTeacher['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli'];

the () are missing altogether, you are calling/invoking a method here.

undefined - because you use console.log twice: in function and when calling the function. Try this way
quickFacts() {
const fact = '${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.';
return fact;
}