Conditional to check the input if it is a number (typeof) in the Setter for numberOfStudents is not working


#1

I have a similar problem, I double checked the code if there are not errors, everything is just fine. No errors related to coding. However, the conditional to check the input if it is a number (typeof) in the Setter for numberOfStudents is not working, it is like the setter is bypassing the conditional and doesnt care what kind of variable is numberOfStudents. 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  {    
     return  console.log('Invalid input: numberOfStudents must be set to a Number.');
    }
  }

  // Methods
  
  quickFacts(){
    return console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.`);
  }
  
  static pickSubstituteTeacher(substituteTeachers) {
    let teachering = Math.floor(substituteTeachers.length * Math.random() -1 );
    
    return substituteTeachers[teachering];  
    
  }
 
};


//Primary School

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


//Middle School

class MiddleSchool extends School{
constructor(){

  }
};


//High School

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

//Inputs

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

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

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

//Results

console.log(lorraineHansbury.numberOfStudents);
lorraineHansbury.quickFacts();

console.log(alSmith.numberOfStudents);
alSmith.quickFacts();

The last two line just to print out the real values of numberOfStudents. Can someone point out where is the problem.

I did some experimentation and made for “typeof” conditionals for testing issues, they worked as they should, but somehow it is not working in the exercise.


Scholl Catalogue, setter not working
#2

Is it necessary to return? The code will still execute but the SCT may not be expecting a return (which will be undefined).


#3

//Parent classs SCHOOOL
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: ${value} 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 randomNumber = Math.floor(Math.random() * (substituteTeachers.length));
return substituteTeachers[randomNumber];
}
}//end of School Class

//Primary Class
class Primary extends School{
constructor(name, numberOfStudents, pickupPolicy){
super(name, ‘primary’, numberOfStudents);
this._pickupPolicy = pickupPolicy;
}
get pickupPolicy(){
return this._pickupPolicy
}
}//end of primary class

//Middle Class
class Middle extends School{
constructor(name, numberOfStudents){
super(name, ‘middle’, numberOfStudents);
}
}//end i fmiddle class

//High Class
class High extends School{
constructor(name, numberOfStudents, sportsTeams){
super(name, ‘high’, numberOfStudents);
this._sportsTeams = sportsTeams;
}
get sportsTeams (){
for (let i = 0; i< this._sportsTeams.length; i++){
console.log(Team ${i+1}. is ${this._sportsTeams[i]})
}
}
};//en of High Class

// primary school instances
const Primary1 = new Primary(‘st joseph’, ‘TEST’, ‘BE ON TIME’)
//Primary1.numberOfStudents = ‘RRR’;

console.log(Primary1)

//sub teacher call for SCHOOL ONLY, //static
//console.log(School.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, ‘James Harden’, ‘Jason Terry’, ‘Manu Ginobli’]));

Anyone know what the problem is here? the numberofstudents setter is not working, but does work if i directly put the code Primary1.numberOfStudents = ‘RRR’ at the end of my code, any suggestions would be greately appreciated