Why isnt my numberOfStudents setter logging my error message? should be logging ‘Invalid input: numberOfStudents must be set to a Number.’ I’ve set the numberOfStudents to string values but it still just prints the string I put in even though its not a number.
Your setter only appears to check if the input is a NaN
value, which strings are not. Therefore if the input is a string it will get through the if statement and not throw the error. You need to add a condition which checks for strings also.
Hi @data5918338421. Welcome to the community!
How do we check for NaN values? There are two methods available in JS called isNaN()
or Number.isNaN()
to check for NaN values. Are you checking for NaN values in the correct way? Or is there something else you need to do in order to check for NaN values in your setter?
Here’s the links to the documentation for the methods I mentioned above:
Refer them for more help!
I’m still not getting it. I have tried everything I can think of and it does not work. To my understanding if value is not a number then print ‘message’. Else print value. The setter does not seem to have any effect on the output no matter what I put in. I feel like that should be working because if it is anything but a number it should print the error I have entered. I am very new to all this and am having a hard time understanding the concepts.
In the code you provided, I can’t find where you use the setter in the first place. Can you provide that, too?
And could you also post an update of your usage of the isNaN method?
you have to click “show original” on the pic of the code. I kept everything the way it is after trying it different ways multiple times.
set numberOfStudents(value) {
if (value.isNaN()) {
console.log(‘Invalid input: numberOfStudents must be set to a Number.’)
} else {
this._numberOfStudents = value; }
I saw the setter, but I didn’t see where you actually call it to set numberOfStudents. Like this:
DumbCollege.numberOfStudents = 450;
I also think that isNaN is not the method you’re looking for, because as @adamgaffney96 wrote, you’re not sorting out strings with it. And as @goku-kun pointed out, you’re not using the correct syntax.
isNaN is only looking for the specific value NaN, a string like ‘450’ doesn’t match.
const DumbCollege = new PrimarySchool(‘Dumb College’, ‘blah’, ‘Students must be picked up by a Parent, guardian, or a family member over the age of 13.’);
I assumed that ‘blah’ would call the setter. And since ‘blah’ is not a number it should print the error statement I put in. I am not at the computer again until tomorrow but will try calling the setter the way you explained and changing my set method to do what I want it too. Thank you for your help! I just started this programming journey and am trying to improve everyday.
This creates an instance of PrimarySchool using the constructor of PrimarySchool for pickupPolicy
and the constructor of the parent class School for name
and numberOfStudents
which you’re calling with super()
.
The setter is used to manipulate the property values on an existing instance. If you call it using the correct syntax, you should be much closer to the desired error message
Happy coding and a successful journey!
Thank you for this! Your statement here makes everything much more clear in understanding the call process overall. You are awesome mirja_t!
This is working just not in the way I intended. lol
I expect the only output to be: ‘Codecademy educates Invalid Input… students at the Expert school level.’
Instead I get:
Invalid Input…
Codecademy educates undefined students at the Expert school level.
undefined
… I’ll take it but not exactly what I wanted lmao
This message you get, because you’re using an invalid type in
college.numberOfStudents = 'this'
That’s what you intended, I guess.
You get this by calling college.quickFacts()
.
The number of students is undefined, because you’re creating the instance:
new School('Codecademy', 'Expert', `${this._numberOfStudents})`
with the undefined value ${this._numberOfStudents}
this
is out of context here.
And you get this:
because you’re logging a console.log():
console.log(college.quickFacts())
console.log(console.log()) // undefined
If you want to log college.quickFacts()
to a console, use the return
keyword inside your quickFacts() method instead of console.log().
Holy crap I finally got it!
I put the if else statement in quickFacts.
now the output is what I expected. Thank You for your help!
There is probably a better way to do this. lol