简体   繁体   中英

Pass an object method an array of strings (Javascript)

The goal is to pass an array of strings (Substitute teacher names) to the 'pickSubstituteTeacher' method and return one random teacher. I cannot figure out how to send an array of strings to the object method and return the random value.

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

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

const school1 = new School('school1', 'two', 233);


let randomTeacher = School.pickSubstituteTeacher['teacher1','teacher2','teacher3'];

console.log(randomTeacher);

您必须在数组周围加上括号以表明该数组是该方法的参数。

let randomTeacher = School.pickSubstituteTeacher(['teacher1','teacher2','teacher3']);

You are sooo close...

Go with this: let randomTeacher = School.pickSubstituteTeacher(['teacher1', 'teacher2', 'teacher3']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM