School Catalogue getter syntax

Hello CA users :stuck_out_tongue:

I’m having some trouble, on the School Catalogue project in learn JS, with (what I think to be) the syntax of my numberOfStudents setter, as it is not passing through the value given when creating a new instance of the primaryScool object… I’m not sure if it because of my conditional, or because I have missed something in its ability to pass through the value as an argument?

Any help would be much appreciated, here is the syntax for the setter method specifically, I will post the entirety of the code if it helps as well!

set numberOfStudents(newNumberOfStudents) {
  if (typeof numberOfStudents !== NaN) {
    this._numberOfStudents = newNumberOfStudents
  } else {console.log("Invalid input: numberOfStudents must be set to a number")}

Warmest regards,
C :upside_down_face:

Hi Callum,
this snippet suggests that you want to check the type of the argument given to the setter, not the setter itself. That means you should write typeof newNumberOfStudents.
You get ‘NaN’ after running an equation with one of its arguments being ‘undefined’, for example. A string would pass the test:
typeof 'str' !== NaN // true
If you just want to check if the argument given to the setter is a number, not a string, you better check if the type of the argument given to the setter equals ‘number’.

1 Like

I initially had something more like if (typeof newNumberOfStudents === number) { but then was doubtful about how number would be read properly, so that clears that up for me somewhat, thank you

Now I am unsure about wether I should have a the conditional separately from the setter method, because the second argument passed into the new PrimarySchool instance isn’t being checked (the one that should be returned as a number)

Thank you for your help, Mirja, you are as ever, incredibly helpful in pointing out details!!

1 Like