Class Assignment School Catalog | Undefined value of a subclass argument

School Catalog

Hello my code doesn’t seem to store the level value of the classes. Below is my code for the assignment;

class School {
  constructor( name, level, numberOfStudents) {
    this._name = name;
    this._numberOfStudents = numberOfStudents;
  }
  
  get name() {
    return this._name;
  }
  get level() {
    return this._level;
  }
  get numberOfStudents() {
    return this._numberOfStudents;
  }
  set numberOfStudents(number) {
    if (typeof number === 'number') {
    this._numberOfStudents = number;
      } else {
        return 'Invalid input: 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 array = substituteTeachers;
    let range = substituteTeachers.length - 1;
    let random = Math.floor(Math.random() * range);
    return array[random];
  }
  
}

class Primary extends School {
  constructor( name, level, numberOfStudents,  pickupPolicy) {
    super(name, level, numberOfStudents)
    this._pickUpPolicy = pickupPolicy;
  }
  
  get pickUpPolicy() {
    return this._pickUpPolicy;
  }
  
}

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

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

I didn’t include the code for the other 2 subclasses since they were irrelevant. Alas when I run this code to get facts about the primarySchool, the school level returns undefined.

Code:

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

Output:

Lorraine Hansbury educates 514 students at the undefined school level.
undefined

I’m an idiot, I did not define the level at the beginning of the superclass’ contents, that’s the problem. After inputting this._level = level; the code fixed itself.

7 Likes

Nice job finding and fixing your mistake! :+1:
You’re not an idiot. We all make mistakes. Every programmer makes mistakes. Learning to debug your own code is a crucial skill. :slightly_smiling_face:

9 Likes

No you aren’t. Just remember a FAIL is a:
First
Attempt
In
Learning

I hope this helps =)

7 Likes