School catalog

hi everybody

I am doing the school catalog project and i am trying to build the constructor for the child classes of primary, middle and high. i wrote the following code:

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

and i get the following error:

/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:40
    super('primary');
    ^

ReferenceError: this is not defined
    at new Primary (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:40:5)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:71:26)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

what is the right way to write a constructor to the level property?
thanks:)
Dorit

if we look at the constructor of School class:

constructor(name, level, numberOfStudents) {
    this._name = name;
    this._level = level;
    this._numberOfStudents = numberOfStudents;
  } 

we can see 3 parameters, so you can call super() one time to provide all the arguments you want.

you can only call super once. By only providing one argument, you only set this._name in School constructor, the others would be undefined.

hi stetim94
thank for the quick answer.
I put all the parent constructor arguments in one super method like this:super(name,'primary',numberOfStudents);
and the previous error message is gone.
but it still wont get the level value when I am calling : lorraineHansbury.quickFacts();
I get the following output:

Lorraine Hansbury educates 514 students at the LEVEL school primary.

it is writing LEVEL instead of primary.
thanks in advance
Dorit

this certainly a good step. You understand why as well?

Please post your full code, then i can help debug the issue.

  1. I think I understand, this Is because you can call super only once.
  2. this 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(numberOfStudents){
    if(typeof numberOfStudents == 'number')
      {
    this._numberOfStudents = numberOfStudents;
      }
    else
      {
        console.log('Invalid input: numberOfStudents must be set to a Number.');
      }
  }
  quickFacts(){
    console.log(`${this._name} educates ${this._numberOfStudents} students at the LEVEL school ${this._level}.`)
  }
  static pickSubstituteTeacher(substituteTeachers){
    const rand = Math.floor(Math.random()*(substituteTeachers.length-1));
    return substituteTeachers[rand];
  }
  
}

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

class Middle extends School{
  constructor(name,numberOfStudents)
  {
    super(name,'Middle',numberOfStudents);
  }
}

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

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.');
lorraineHansbury.quickFacts();
School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);
const alSmith = new High('Al E. Smith',415,['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);
alSmith.sportsTeams;

primary is written, its after school at the end of the sentence.

looking at the quickFacts method:

console.log(`${this._name} educates ${this._numberOfStudents} students at the LEVEL school ${this._level}.`)

the only change you need to make is a small tweak to the string logged .

hi
I am so sorry for this questions, I guess I got a little tired.
thanks for your patience.
Dorit

no problem

Sometimes you can just stare at a problem which is so simple, but you just don’t see it. been there, done that, more times then i would like to admit :wink:

1 Like