School Catalouge Project

Hi,

I’m working through the School Catalouge project …

https://www.codecademy.com/courses/introduction-to-javascript/projects/school-catalog

I’m trying to understand a console.log output being generated by my code…

class School {
  constructor(name, numberOfStudents) {
    this._name = name;
    this._numberOfStudents = numberOfStudents;
    this._level = [];  
  }
  get name() {
    return this._name;
  }
  get level() {
    return this._level;
  }
  get numberOfStudents () {
    return this._numberOfStudents;
  }
  set numberOfStudents (numberOfStudents) {
    if(isNaN(numberOfStudents)) {
      this._numberOfStudents = numberOfStudents;
  } else {
      return console.log('Invalid input: numberOfStudents must be set to a Number.');
  }
  }
  quickFacts () {
    return console.log(`${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.`);
  }
  
  static pickSubstituteTeacher (substituteTeachers) {
    let myIndex = Math.floor(substituteTeachers.length * Math.random());
    let substituteTeacher = substituteTeachers[myIndex];
  }
}

class Primary extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name);
    this._numberOfStudents = numberOfStudents;
    this._pickupPolicy = pickupPolicy;
    this._level = 'primary';
  }
  get pickupPolicy() {
    return this._pickupPolicy;
  }
}


+++


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());

When I run the above the console generates the following output…

Lorraine Hansbury educates 514 at the primary school level.
**undefined**

My question is… where does the ‘undefined’ come from in the above output? I can’t work out why it is being generated. I guess the answer is probably fairly simple but I can’t work it out :slightly_smiling_face:

Many thanks.

Hello @code4551185480. I belive the problem is that with the quickFacts method, you’re not returning anything, rather console.loging it. That means that when you console.log the result of that function:

You’re really just printing the nothing that was returned from that function.
I hope this helps!

Many thanks! Yes, I can see the problem now. In effect, I’ve got a nested ‘console.log’ command.

What I have now is …

 quickFacts () {
    return `${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.`;
  }


console.log(lorraineHansbury.quickFacts());


Thanks for your help.

1 Like