Array Method returning single letter instead of array element?

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/school-catalog

Hi Im having a small issue with code below, static method pickSubstituteTeacher, as part of the School class, is returning a single letter instead of a string, any idea why? Have tried searching for similar issues but I think I’m missing something!

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(newNumberOfStudents) {
    if (typeof newNumberOfStudents === 'Number') {
      this._numberOfStudents = newNumberOfStudents;
    } 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(substituteTeachers) {
const randomNumber = Math.floor(Math.random()*substituteTeachers.length);
console.log(substituteTeachers[randomNumber]);
}


}

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'Primary', numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }

  get pickupPolicy() {
    return this._pickupPolicy;
  }
}

class HighSchool extends School {
  constructor(name,numberOfStudents,sportsTeams) {
    super(name, 'High', numberOfStudents);
    this._sportsTeams = sportsTeams;
  }

  get sportsTeams() {
    return this._sportsTeams;
  }
}

let lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

lorraineHansbury.quickFacts();

School.pickSubstituteTeacher('Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli');

let alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);

console.log(alSmith.sportsTeams);


class MiddleSchool extends School{
  constructor(name, numberOfStudents,averageTestScores,schoolOverview) {
    super(name, 'Middle', numberOfStudents);
    this._averageTestScores = averageTestScores;
    this._schoolOverview = schoolOverview;
  }

  get averageTestScores() {
    return this._averageTestScores;
  }

  get schoolOverview() {
    return this._schoolOverview;
  }

}

we could start debugging this by adding a .log() to inspect/see the parameter value:

static pickSubstituteTeacher(substituteTeachers) {
    console.log(substituteTeachers);

we see a string. Is this what you expect?

1 Like

Ah of course you are right, the input to the method was not encapsulated in square brackets so wasn’t actually an array! Makes perfect sense, thank you :slight_smile:

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