Rengga Dev – Math.random()
is an API in JavaScript. It is a function that gives you a random number. The number returned will be between 0 (inclusive, as in, it’s possible for an actual 0 to be returned) and 1 (exclusive, as in, it’s not possible for an actual 1 to be returned).
Images are stored in an array. A number is generated and multiplied by the number of images in the array via array.length
. Then Math.floor
rounds the value to a round number and sets the image src in the HTML when the page is loaded or the button is clicked.