Premier League Match Historian

Mixed Messages Project - Premier League Match Historian

I was really working on trying to refactor the code afterwards without effecting readability.

If you have a second to leave feedback I would greatly appreciate it! Thank you!


There is an issue with random index generation in your code. And this issue throws an exception sometimes (not gaps, as you comment).

Let’s look at line const randI = Math.floor(Math.random() * matchArray.length-1); in edge cases:

  1. If matchArray contains only single record, inside Math.floor we get numbers from 0 * 1 - 1 = -1 (min for Math.random is 0) to 1 * 1 - 1 = 0. So we can get negative index, accessing array with negative index throws exception.
  2. Another issue that we will get 1 from Math.random very rarely. Logically, for 2 records in matchArray when we get more than 0.5 from Math.random we have to use second element (with index 1). But in this code we get 1 in result only when Math.random returns 1. Another random index generation (Math.ceil(Math.random() * responses.length-1)) has similar problems but from another side.

Test your code with matchArray contains 0, 1 and 2 items and try to fix error.

Good work!

Some fix to my message: you will never get 1 from Math.random() as documentation says: Math.random() - JavaScript | MDN. So in item 2 you will never get last match in array.