School catalogue Project

I am getting an extra “undefined” value in my console.log on calling quickFacts on my instances. Couldn’t figure out the bug!!

class School {

constructor(name, level, numberOfStudents) {

this._name = name;

this._level = level;

this._numberOfStudents = numberOfStudents;

}

get name() {

return this._name;

}

get level() {

return this._level;

}

get numberOfStudents() {

return this._numberOfStudents;

}

set numberOfStudents(num) {

if(typeof num === 'number') {

this._numberOfStudents = num;

}

else {

  console.log('Invalid input: numberOfStudents must be set to a number.')

}

}

quickFacts() {

console.log(`${this.name} educates ${this.numberOfStudents} students at the ${this.level} school level.`);

}

static pickSubstituteTeacher(substituteTeacher) {

let randomIndex = Math.floor(Math.random() * substituteTeacher.length);

return substituteTeacher[randomIndex];

}

}

class Primary extends School {

constructor(name, numberOfStudents, pickupPolicy) {

super(name, 'primary', numberOfStudents);

this._pickupPolicy = pickupPolicy;

}

get pickupPolicy() {

return this._pickupPolicy;

}

}

class Middle extends School {

constructor(name, numberOfStudents) {

super(name, 'middle', numberOfStudents);

}

}

class High extends School {

constructor(name, numberOfStudents, sportsTeam) {

super(name, 'high', numberOfStudents);

this._sportsTeam = sportsTeam;

}

get sportsTeam() {

return this._sportsTeam;

}

}

const lorraineHansbury = new Primary(‘Lorraine Hansbury’, 514, ‘Students must be picked up by a parent, guardian, or a family member over the age of 13.’)

console.log(lorraineHansbury.quickFacts())
//Lorraine Hansbury educates 514 students at the primary school level.
undefined

Your quickFacts() method already logs the statement to the console and returns nothing, so there’s no need to log the call to the console. As the return value from the method is undefined, logging it to the console logs undefined. You can either change the method to return the statement, or you can just call the method without a console.log().

1 Like

Thanks a lot for clarifying.