jeudi 15 juillet 2021

If Statement not runing inside setter

im fairly new to programing (about a week lol), but here is a boiled down peice of script i wrote, and i cant seem to get it to output correctly. for the number of students, there is an if statement that i placed inside the corresponding setter. no matter what i do, if seems that the if statement just gets bypassed. not sure whats happening but ive been staring at this for like an 2 hours now and re written it 3 times. can someone explain what im doing wrong>

class School {
    constructor(numberOfStudents){

      this._numberOfStudents = numberOfStudents;
    }

    get numberOfStudents(){ return this._numberOfStudents}
    set numberOfStudents(value){ 
      if(typeof value !== 'string'){
        this._numberOfStudents = value
      } else {
        console.log('Invalid input: numberOfStudents must be set to a Number.')
      }
    } 
  };

  const highSchool = new School('many');
  console.log(highSchool.numberOfStudents);

also to note; i belive the output should be this as the variable inputed is a 'string'

console.log('Invalid input: numberOfStudents must be set to a Number.')

Aucun commentaire:

Enregistrer un commentaire