Help with School Catalogue

Good evening everyone, I’m currently working on the School Catalogue prject and I’ve run into a bit of a snag.

I I’m currently working on step 5 of the assignment where I have to create a setter function (set numberOfStudents) to set the number of students in a school class. for some odd reason whenever I call this function on an instance (x) of the school object, I get an error in the console stating that

“TypeError: x.numberOfStudents is not a function”.

However, if I was to change the code of the function from set numberOfStudents to setNumberOfStudents, my function executes without issue.

Could anyone please take a look at my code and provide an explanation for the behavior that I’m experiencing?

here is my code…

  constructor(name, level, numberOfStudents){
    this._name = name;
    
    if(level === 'primary'){
      this._level = level;
    }
    else{
      if(level === 'secondary'){
        this._level = level;
      }
      else{
        if(level === 'high'){
          this._level = level;
        }
        else{
          this._level = 'primary';
        }
      }
    }
    this._numberOfStudents = numberOfStudents;
  }
  
  //getters
  get name(){
    return this._name;
  }
  
  get level(){
    return this._level;
  }
  
  get numberOfStudents(){
    return this._numberOfStudents;
  }
  
  //setters
  set numberOfStudents(studentPop) {
    if (typeof studentPop === 'number')
    {
      this._numberOfStudents = studentPop;
    } 
    else{
      console.log('Invalid input: numberOfStudents must be set to a Number.');
      return;
    }
  }
}

const x = new school('McBride Secondary', 'secondary', 20000);

x.numberOfStudents(3000);
type or paste code here

Thank you for taking the time to read this!!!

@enwafor Did you exclude the top line of your code?

This is not how to use a setter.
Try:

x.numberOfStudents = 3000;
1 Like

I did…my bad :sweat_smile:

Here you go:

class school{
  constructor(name, level, numberOfStudents){
    this._name = name;
    
    if(level === 'primary'){
      this._level = level;
    }
    else{
      if(level === 'secondary'){
        this._level = level;
      }
      else{
        if(level === 'high'){
          this._level = level;
        }
        else{
          this._level = 'primary';
        }
      }
    }
    this._numberOfStudents = numberOfStudents;
  }
  
  //getters
  get name(){
    return this._name;
  }
  
  get level(){
    return this._level;
  }
  
  get numberOfStudents(){
    return this._numberOfStudents;
  }
  
  //setters
  set numberOfStudents(studentPop) {
    if (typeof studentPop === 'number')
    {
      this._numberOfStudents = studentPop;
    } 
    else{
      console.log('Invalid input: numberOfStudents must be set to a Number.');
      return;
    }
  }
}

const x = new school('McBride Secondary', 'secondary', 20000);

x.numberOfStudents(3000);```
1 Like

Wow wow wow lol…that did it! Thank you!

so it was clearly a syntax error on my part. I guess i’m having trouble keeping my syntax straight.

Is that the standard for setter methods in js? again, It could very much so be me getting my syntax confused so current me if I’m wrong, but I feel like i’ve seen ‘set’ functions where the parenthesis are used.

That is standard syntax for invoking the setter. The reason it worked when you changed the setter name to setNumberOfStudents(studentPop) from set numberOfStudents(studentPop) is it went from a setter denoted by the key word set to a method called setNumberOfStudents(studentPop) which accepted the argument you supplied inside the parenthesis when you called it. Hope this helps!

1 Like