# School Catalogue Project pickSubstituteTeacher static method

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

I did this for task #7:

``````  static pickSubstituteTeacher(substituteTeachers) {
substituteTeachers = [];
const randNum = Math.floor(Math.random() * (substituteTeachers.length - 1));
return substituteTeachers[randNum];
}
``````

But I was wondering if it’s wrong because in the video walkthrough, they did:

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

``````

which doesn’t have the `substituteTeachers = [];` part or the `.length - 1` part.

overwriting the parameter completely is rarely a good idea. Now if we call the method with an argument ( a list of substitute teachers):

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

the array/argument is passed to the parameter (`substituteTeachers`) and then overwriting by an empty array. So we no longer have substitute teachers to choice from

doing `-1` means you will never get the last teacher in the list/array. Kind of unfair, no?

`Math.random()` generates a number between 0 and 1, but never `0` or `1` itself.

so the highest possible value is 0.9999999999999 (not sure how many decimal places, but you get the idea)

so if we have 5 substitute teachers: 0.999 * 5 = 4.995. When floored/rounded down is 4, which is the highest index. So subtracting one is not a good idea. Like I said, the last teacher/element in the list then never stands a chance

1 Like

Thank you very much. That is very helpful. I thought the Math.random() includes the 0 but not the 1. You said it doesn’t include either?

I am afraid I need to stand corrected, according to the documentation (link):

The `Math.random()` function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1)

so you are right. But point still stands about subtracting 1 still is a bad idea. Still means we can’t get the last element from the array

1 Like