School Catalogue - condition to set numberOfStudents not working


#1

The problem in question is: https://www.codecademy.com/courses/introduction-to-javascript/projects/school-catalog?action=resume_content_item

my code as follows:

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(value){
    if(typeof value === 'number'){
    	this._numberofStudents = value;
    }else{
      console.log('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){
    const randSub = Math.floor(Math.random() * substituteTeachers.length);
    return substituteTeachers[randSub];
  }
}//END SCHOOL

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

class MiddleSchool extends School{
  constructor(name,numberOfStudents){
    super(name, 'middle', numberOfStudents)
  }
}

class HighSchool extends School{
  constructor(name,level,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();

The console shows
Lorraine Hansbury educates 514 students at the primary school level.

That’s all fine, but if I replace the number of 514 with a string like ‘test’ it gives me
Lorraine Hansbury educates test students at the primary school level.

I’m not sure why the setter doesn’t care if it’s a number or a string. I’ve searched these forums for a while testing other people’s code when I find it and they have the same issue whether they know it or not.
What gives? I can’t get that console.log from the if statement no matter what I do.


#2

The setter is not involved in the instantiation process, so if we pass a string in new class instance argument, the property will be set with that string.

Chances are good that your setter works properly.


#3

Hmm ok, I feel a little bit more lost. So, if the setter is working properly, and the instructions were to make sure that only a number was set, how do I … do that? I checked out the hint and I did it properly as the hint eluded to, the guy in the video did a different bit of code, but testing his code gave the same result.

What is going wrong here?


#4

What happens if you run,

lorraineHansbury.numberOfStudents = 'test'

?


#5

Ok, odd when I do that the proper console.log works and spits the error that it’s not a number

Why does this get to get away with it

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

if I change it to

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

#6

As explained above, the setter does not have a role in creating the instance, and there is no validation prior to instantiation. We have no access to the setter in the constructor, but could create a static method for validating the instance arguments. That, however is beyond the purview of this exercise.


#7

Ok, I think I understand now. Thanks for the patience!