How do I properly call a setter method that is inside a class?

Hi,

I’m working on the second project (school catalog) of the Intermediate JavaScript course. I have a setter in the parent class School which changes the number of students in the school, however when I try to use to change the number of a school I am getting an error message:
console.log(lorraineHansbury.numOfStudents(323))
^

TypeError: lorraineHansbury.numOfStudents is not a function
at Object. (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:60:30)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Here is the full code:

class School { constructor(name, level, numOfStudents) { this._name = name; this._level = level; this._numOfStudents = numOfStudents; } get name() { return this._name; } get level() { return this._level; } get numOfStudents() { return this._numOfStudents; } set numOfStudents(value) { if (typeof value === "number") { this._numOfStudents = value; } else { console.log("Invalid input: numberOfStudents must be set to a Number."); } } quickFacts() { console.log( `${this.name} educates ${this.numOfStudents} students at the ${this.level} school level.` ); } static pickSubstituteTeacher(substituteTeachers) { const randomIndex = Math.floor(Math.random() * substituteTeachers.length); return substituteTeachers[randomIndex]; } } // Leave the 'level' parameter out because all Primary Schools have the same level! class PrimarySchool extends School { constructor(name, numOfStudents, pickupPolicy) { super(name, "primary", numOfStudents); this._pickupPolicy = pickupPolicy; } get pickupPolicy() { return this._pickupPolicy; } } const primarySchool1 = new PrimarySchool('Escola Anchieta', 544, 'Free Pickup'); console.log(primarySchool1) class HighSchool extends School { constructor(name, numOfStudents, sportsTeams) { super(name, 'high school', numOfStudents); this._sportsTeams = sportsTeams; } get sportsTeams() { console.log(this._sportsTeams); } } const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.'); console.log(lorraineHansbury.numOfStudents(323))

I can’t figure out where is my error.

You do not need to invoke a setter like a function. With a setter you assign a property:

lorraineHansbury.numOfStudents = 323;

With your getter you can then check if the property has been set by addressing it like a property:

console.log(lorraineHansbury.numOfStudents);
1 Like