School Catalogue Project

Hi everyone,

I really need some help with this School Catalogue project in Javascript. I have followed the tutorial to the point where I need to make an instance of a class. However when I fill in the necessary arguments following the tutoral, JS is telling me that some of the variables are ‘undefined’. I’m not sure why that happens in my code, when it didn’t happen in the video tutorial. Could someone tell me what I’m doing wrong?

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

////////////////////////////////////////
// Child class: Primary School 
class Primary extends School {
  constructor(name, level, numberOfStudents, pickupPolicy){
    super(name, 'Primary', numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }
  
  get pickupPolicy(){
    return this._pickupPolicy;
  }
  
}

////////////////////////////////////////
// Child class: Middle School 
class Middle extends School {
  constructor(name, level, numberOfStudents){
    super(name, 'Middle', numberOfStudents);
  }
}

////////////////////////////////////////
// Child class: High School 
class High extends School {
  constructor(name, numberOfStudents, sportsTeams){
    super(name, 'High', numberOfStudents);
    this._sportsTeams = sportsTeams;
  }
  
  get sportsTeams(){
    return this._sportsTeams;
  }
}

////////////////////////////////////////
// TESTS:

const lorraineHansbury = new Primary('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

console.log(lorraineHansbury);

Output code:

Primary {
  _name: 'Lorraine Hansbury',
  _level: 'Primary',
  _numberOfStudents: 'Students must be picked up by a parent, guardian, or a family member over the age of 13.',
  _pickupPolicy: undefined }

School Catalogue Project

QUICK EDIT NOTE: The highschool instance appears to work. The Primary and Middle School don’t.

I see a couple of issues. The first is unrelated to your described problem, but in your setter method for setting the number of students, you have an errant return.

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

You don’t want to return the value to the caller. You want to set the value, so leave out the return.

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

Take note of the parameters the constructor is accepting: name, level, numberOfStudents, pickupPolicy as opposed to the parameters you are passing to the constructor:

const lorraineHansbury = new Primary('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

If you’re still stuck, click the spoiler:

If you remove ‘level’ from your constructor parameters, it should work.

Thank you so much for your help - it works! And I see that is the reason my ‘High’ school instance worked over the other two. I just hadn’t noticed the connection.

For some reason I thought you had to pass the same parameters in the constructor of the inherited class, that are in the constructor of the parent, for it to work.

I have also moved the return in my setter. Thank you for spotting that I appreciate it!

1 Like

You are most welcome! Happy coding!

Hello,
I also have a problem with a setter which doesn’t want to realize the if else check… I’ve spent a lot of time checking what’s wrong and I can’t spot any errors… I’ll be very grateful for pointing out what’s wrong in my code that causes the problem.
Here is my 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
}
set numberOfStudents(newNumberOfStudents) {
if (typeof newNumberOfStudents === ‘number’) {
this._numberOfStudents = newNumberOfStudents
} else {
console.log(‘Invalid input: numberOfStudents must be set to a Number.’)
}
}
quickFacts() {
console.log(${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.)
}

static pickSubstituteTeacher(substituteTeachers) {
console.log( substituteTeachers[Math.round(Math.random() * substituteTeachers.length -1)])    

}
}

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, level, numberOfStudents, sportsTeams) {
super(name, ‘high’, numberOfStudents)
this._sportsTeams =
}
get sportsTeams() {
return this._sportsTeams
console.log(this._sportsTeams)
}
}

const lorraineHansbury = new PrimarySchool(‘Lorraine Hansbury’, 465, ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’)
console.log(lorraineHansbury)
//why doesn’t the setter work and checking the input type is not working?..
lorraineHansbury.quickFacts()

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’])

When I run your code, I get this:

image

What result am I supposed to get?

You caught a flaw in the logic. Good job! If you include an invalid argument ie. ‘elephant’ instead of a number for the numberOfStudents in the constructor, the setter is bypassed, and the constructor method assigns whatever the value is to the numberOfStudents property.

const lorraineHansbury = new PrimarySchool(‘Lorraine Hansbury’, 'elephant', ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’)
console.log(lorraineHansbury)

Logs this to the console:

PrimarySchool {
_name: ‘Lorraine Hansbury’,
_level: ‘primary’,
_numberOfStudents: ‘elephant’,
_pickupPolicy: ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’ }
Lorraine Hansbury educates elephant at the primary school level.
Jamal Crawford

You can correct this behavior by modifying the constructor for each school to use the setter instead of including the numberOfStudents in the super(arguments) statement. For example:

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'primary'); //numberOfStudents removed
    this._pickupPolicy = pickupPolicy;
    this.numberOfStudents = numberOfStudents; //use the setter instead
  }
  get pickupPolicy() {
  return this._pickupPolicy;
  }
}

Logs:

Invalid input: numberOfStudents must be set to a Number.
PrimarySchool {
_name: ‘Lorraine Hansbury’,
_level: ‘primary’,
_numberOfStudents: undefined,
_pickupPolicy: ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’ }
Lorraine Hansbury educates undefined at the primary school level.
Jason Terry

Hope this helps!
P.S. I did see a couple of other issues. If you don’t find them, I’d be happy to help.

Hi,

Thank you very much for your reply! I haven’t finished the whole project yet, co I’m aware of the other impersfections :wink: I just concentrated on solving this puzzle for now. I’m very grateful for your help! :slight_smile:

I need help, I’ve finished project and get log
{name} educates {numberOfStudents} students at the {level} school level. [ 'Baseball', 'Basketball', 'Volleyball', 'Track and Field' ] What is error in my code? 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(value){ if (typeof value === 'Invalid input: numberOfStudents must be set to a Number.'){ } else { this.numberOfStudents = value; } } quickFacts() { console.log('{this.name} educates {this.numberOfStudents} students at the {this.level} school level.’);
}
static pickSubstituteTeacher(substituteTeachers){
const randInt = Math.floor(Math.random() * substituteTeachers.length);
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;
}
}
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();

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

Hey @ljv66, welcome to the forums!

Did you remember to use backticks when you use the ES6 string interpolation?

Here is my code snippet :slight_smile:

It took quite a bit of time to make the Task 19

  • Create a class called SchoolCatalog that holds a collection of schools. Create an instance of SchoolCatalog for primary, middle, and high schools.

SchoolCatalog class
I followed the below logic

  • Create a constructor with parameter ‘name’.
  • Constructor has a School parameter which is initially set to empty array
  • The class has getters and setters for just School Property
  • Now I instantiate an object of ‘SchoolCatalog’ class
  • Then use this object with setter method to add schools into the school property of SchoolCatalog class

What I would however wanted to achieve was create a property inside constructor of SchoolCatalog which could have School Object having Key-Value pairs, something like this

class SchoolCatalog {
  constructor(level,school){
    this._school = {
          name: school,
          level: level
    };
  }
  }

But this didn’t work, not sure if anybody could help me with this approach ? or IF there is any flaw with this approach ?

Thanks