At line 8 the code tries to access the first element (index=0) of an empty array.

The code should test that there are elements in the array before trying to access them.
The .length attribute can...