There are some things in your code i would like to point out
But to your Question
functions take parameters, so could take one param for an array of images
function (imgArr) {...
With this you have access to the passed parameter throug imgArr
function can return values, so taken Math.random
we can go on
function (imgArr) {
return imgArr[Math.floor(Math.random() * imgArr.length)]
}
You don't have to "Dim" Array, and predefine the Elements you gonna put in, you can just do sth. like
var imgArr = ["img01.png","img02.png",...]
And var arr=[]
would be equals var arr = new Array()
So calling this function with your Array couldlook like
var img = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png"]
function imgRandom(imgArr) {
return imgArr[Math.floor(Math.random() * imgArr.length)];
}
console.log(imgRandom(img));