Scholl Catalogue, setter not working


#1

Hi everyone,

I am struggling with the setter in the school catalogue exercise.
I understand how it works I think but I cannot figure out why it does not work.

As you can see I put a numberOfStudents != number on purpose in the end, but the console does not log anything even if I set the numberOfStudents as a string.

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(NumberOfStudentsIn){
    this._numberOfStudents = NumberOfStudentsIn;
   if(typeof NumberOfStudentsIn === 'Number'){
      this._numberOfStudents = NumberOfStudentsIn;
    } else {
      console.log(`Invalid input: ${this._numberOfStudents} must be set to a Number.` );
    }
  }
} 

const cClaudel = new School('test name', 'test level, 'rer');

EDIT:
I passed this:
cClaudel.numberOfStudents = ‘rer’;

and the setter worked! why doesn’t it work when I entered my variable?


#2

It would make better sense if the user’s input was shown here…

`Invalid input: ${numberOfStudents} must be set to a Number.`

#3

2 posts were split to a new topic: Conditional to check the input if it is a number (typeof) in the Setter for numberOfStudents is not working


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

Im having the same problem, the value that i enter into my instance creation gets put up no matter what, did u get what the problem was?


#5

Please show us your code, and include a link to the exercise page as well as any error messages.