Setter in "School Catalogue" Project

I noticed in this project that we place a setter in the School parent class, but it doesn’t seem as useful as I would have expected. It’s supposed to log an error message if we attempt to set the number of students to anything but a number, but it doesn’t catch such an error if it occurs when declaring a new object. For example:

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

lorraineHansbury.quickFacts() // logs 'Lorraine Hansbury educates test students at the primary school level.'

Notice that ‘test’ is not a number, yet we don’t get an error message. The only time we actually get an error is if we try to change that property specifically, via the defined setter:

lorraineHansbury.numberOfStudents = 'test' // logs the error message

I guess my main question is, is there some way to set up a conditional with classes that will only accept certain data when we’re declaring the new object? For instance, like I’d expected in the first example?

1 Like

We could start by making the constructor use the setter? This way, we would get an error message

However, this represents us with a problem. The underlying property (_numberOfStudents) might now not be set, so you get undefined.

I will leave this for you to think about, I am curios to hear your thoughts and then help you further before I spoil all the fun :wink:

2 Likes

Ah, I see. At first I couldn’t figure out how to implement what you were saying because I didn’t know the parent setter method would hoist up to allow it to be used in the parent constructor, since the setter is defined after the constructor. And yeah, I see the “undefined” problem you mentioned when I call the .quickFacts() method–makes sense.

For something like numberOfStudents, I can see using the parseInt method to convert any string integers to number intergers. But that still leaves you with the problem of NaN if you pass in something that’s not a number.


this.numberOfStudents = parseInt(numberOfStudents);

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

alSmith.quickFacts(); // Logs Al E. Smith educates NaN students at the high school level.

The problem I’m foreseeing is that, unless you wrote the code for something like this and remember the datatype that belongs in each constructor parameter, it seems like it’s hard to control what data you’re getting–especially when you start expecting things like arrays. I’m still very new at this and I’m sure I’ll learn better ways, but these are the kinds of questions I’m asking myself at this point.

`... educates ${isNaN(this.numberOfStudents) ? `unknown number of` : this.numberOfStudents}.`

If the number value given is not a number, should the instance be rejected, or just set to NaN?

1 Like

Yep, I also thought of doing it that way. I like the “unknown number of”, although if you keep in NaN, it’s more clear that there is an error with the data.

1 Like

The value would still be NaN, but the string representation would be unknown number of.