Help with School Catalogue project


#1

https://www.codecademy.com/courses/learn-javascript-classes/projects/school-catalog?course_redirect=introduction-to-javascript

Can you help with task 7?

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.

static pickSubstituteTeacher(substituteTeachers) {
substituteTeachers = [];
let randomTeacher = Math.floor(substituteTeachers.length * Math.random() - 1)
this.substitueTeachers[randomTeacher];

}

I can’t figure out who do it, thanks!


#2

You’ve got most if it right. Just don’t set substituteTeachers equal to an empty array, and remember to return the result at the end.

static pickSubstituteTeacher(substituteTeachers) {
    let randomTeacher = Math.floor(substituteTeachers.length * Math.random() - 1)
    return substitueTeachers[randomTeacher];
}

#3

Hello slinkyfran,

I had the same problem as you.
Now i wrote something like this:

static pickSubstituteTeacher(substituteTeachers){
let arr=[substituteTeachers];
let i=Math.floor((arr.length-1) * Math.random());
console.log(arr[i]);
}

Call:

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

I hobe it will help you :slight_smile:


#4

Hi @jkba2017,

The instructions specify that the pickSubstituteTeacher method should return its result rather than only display it.

Essentially what it needs to do is:

  • choose, at random, an index of the substituteTeachers array that it receives as an argument.
  • return the substitute teacher’s name that is stored at that index.

You can use console.log to display the result of calling the method.

In designing such a method, we could have chosen to have it display a result instead of returning it. However, returning it affords us more flexibility. For example, we could pass the result of calling the method directly to a database system of substitute teachers to extract additional information about the particular teacher that was chosen.


#5

Can I just ask, as a beginner and new to this course, where did you get the idea for “randomTeacher” as it is not mentioned in the instructions?


#6

I don’t think you need -1 after Math.random because if Math.random returns 0 than your randomTeacher index will be -1


#7

Left that one in by accident… Good catch.


#8