Let’s jump straight into an example:
Inside this function:
- We set the minimum number to 0. This is because arrays indexes start at 0.
- Using the length property, we worked out what the last index will be. This is our maximum number.
- Once we figured out the minimum and maximum value, then generated a random integer value between the minimum and maximum values. To do this, we used the Math.floor() method and Math.random() methods.
- Finally, we returned the array value that corresponds to the integer that we generated.
And that’s it!