Learn Intermediate JavaScript: School Catalogue

Hi! This is my first post on codecademy.

I finished every task and the outputs did not have any problem. However, I tried to check the output for task 7, which is: Under .quickFacts(), create a static method named pickSubstituteTeacher. This method will receive one parameter, named substituteTeachers. The substituteTeachers parameter will take an array of strings.

Inside the method, randomly generate a whole number between 0 and one less than the length of the substituteTeachers array. Use this number to access and return the substitute teacher at that randomly generated index.

I am receiving an error message that says “TypeError: Cannot read property ‘length’ of undefined
at Function.pickSubstituteTeacher”

This is my code for the project. Could you possibly explain to me what I did wrong as I constructed the function .pickSubstituteTeacher()? The relevant part of my code to this question is in bold text. Thank you in advance!

class School {
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level,
this._numberOfStudents = numberOfStudents;

get name() {
return this._name = name

get level() {
return this._level = level

get numberOfStudents() {
return this._numberOfStudents

quickFacts() {
console.log(${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.)

static pickSubstituteTeacher(substituteTeachers) {
** const randomIndex = Math.floor(Math.random() * substituteTeachers.length);**
** return substituteTeachers[randomIndex];**
** };**

set numberOfStudents(newNumberOfStudents) {
if(typeof newNumberOfStudents === ‘number’) {
} else {
console.log(‘Invalid input: numberOfStudents must be set to a Number.’)

//Subclass Primary
class PrimarySchool extends School {
constructor(name, numberOfStudents, pickUpPolicy) {
this._numberOfStudents = numberOfStudents
this._level = ‘primary’;
this._pickUpPolicy = pickUpPolicy;
get pickUpPolicy() {
return this._pickUpPolicy

//Subclass HighSchool
class HighSchool extends School {
constructor(name, numberOfStudents, sportsTeams) {
this._numberOfStudents = numberOfStudents
this._level = ‘high’;
this._sportsTeams = sportsTeams

get sportsTeams() {
return 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.’);

School.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, ‘James Harden’, ‘James Terry’, ‘Manu Ginobli’]);


I think the last function call is missing the list of teachers.
So when School.pickSubstituteTeacher() is called it has no data to compute a length.

Also when pasting code in the forum, it’s a good idea to format it by enclosing the code with 3 back tics in the beginning and end so the code gets formatted correctly vs text.

Hi! Thank you for your comment. I will make sure to check the formatting in the future as well.

So even if I created an array in the School.pickSubstitueTeach(), it would not go through the static pickSubstituteTeacher(substituteTeachers)?

If that’s the case, how should I fix my code to let the input go through the pickSubstituteTeacher function?

Well you’re making a function call without passing in data. So that’s the problem I see.
In the first call you do it correctly:

In the second call it’s missing any data also missing a semicolon at the end, so that’s why an error is returned:

So to fix it just put an array of teachers in the second call as well.

One other thing I noticed is that your missing the back tics for string interpolation in your console.log call.
console.log(${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.));

Should be console.log(`${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.)`);

Understood, thank you so much! Have a great day :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.